Get the Most out of WebGrid in ASP.NET MVC

My first MSDN Magazine article is up: Get the Most out of WebGrid in ASP.NET MVC. So, if you want to find out how to make the WebGrid component from WebMatrix fit in with ASP.NET MVC then take a peek….

Thanks to Carl Nolan and Simon Ince for reviewing the article 🙂

**UPDATE - the helpers are now up on NuGet: **

Comments (15)
  1. Haroon says:

    I read your article, it was well written and very informative.

  2. Stuart Clarke says:

    Good job!  Congratulations on your first publication.  Many more to follow I hope.

  3. Graeme says:

    Good article, but what is the SafeCast method that you are using? I am guessing some extension method, but I haven't found any such method in the framework, and a Google search provides numerous hits, none of which appear to be an exact match.

  4. stuartle says:

    Hi Graeme,

    the SafeCast was a little extension method that slightly simplified the code listing by removing a null check. It's in the code download under InfrastructureEnumerableExt

    public static class EnumerableExtensions


           public static IEnumerable<TTarget> SafeCast<TTarget>(this IEnumerable source)


               return source == null ? null : source.Cast<TTarget>();



    – Stuart

  5. Graeme says:

    Thanks Stuart. Somehow I managed to completely miss the fact that there was a download.

  6. Bhargav Soni says:

    Nice Article.

    Can you please explain how to edit WebGrid Column values.

    i.e i want user insert some values in our Gridview and on basis of values i have to display some calculation in gridview.

  7. stuartle says:

    @Bhargav, to enable adding data you would need to render a set of input elements. You could then either add the data to the table in javascript (including performing the calculations), or you could send the data up to the server and retrieve the update to the grid (with calculations performed on the server)

    – Stuart

  8. Siva Ramaswami says:

    Great article Stuart. Is there a way to use WebGrid (or any other equivalent control is ASP.Net MVC) to select multiple rows ?



  9. stuartle says:

    @Siva, you could render the grid inside a <form> and render a custom column in the grid that is a checkbox. Submitting the form would then include any checkboxes that were selected. This wouldn't work across pages in the grid, but aside from that should be a relatively simple solution

    – Stuart

  10. Kevin says:

    Stuart – this looks awesome! Question : do you know if it is compatible with MVC 4?

  11. stuartle says:

    Kevin, I've not fully tested it by any means, but it worked in the ASP.NET MVC 4 project that I included it in a few days ago

    – Stuart

  12. Preeti says:

    Hey Stuart, Nice Article. I was wodering if i could limit the user to check upto 3 checkboxes across different pages. I somehow got it working but it works for each page and not across. (when i move to 2nd page all the 1st page selections are erased). Please briefly let me know how could i deal with this.



  13. Preeti says:

    Another quick question of mine is could we reverse sort?( in the code and not on the address bar) I am doing a default sort on totalVotes column and it shows up in ascending order but i wanted the descending order, can i choose the order??



  14. Girish says:

    It is a nice article. Can a scroll bar be added to the grid?


    Girish V

  15. stuartle says:

    @Girish. Have you tried setting the height of the grid element (or one of its container elements)?

Comments are closed.

Skip to main content