Stuart Leeks

Stuart Leeks - Application Development Consultant

Get the Most out of WebGrid in ASP.NET MVC

Get the Most out of WebGrid in ASP.NET MVC

  • Comments 15

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…. http://msdn.microsoft.com/en-us/magazine/hh288075.aspx.

Thanks to Carl Nolan and Simon Ince for reviewing the article :-)

**UPDATE - the helpers are now up on NuGet: http://blogs.msdn.com/b/stuartleeks/archive/2012/01/24/webgrid-extensions-on-nuget.aspx **

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

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

  • 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.

  • 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 Infrastructure\EnumerableExt

    public static class EnumerableExtensions

       {

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

           {

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

           }

       }

    - Stuart

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

  • 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.

  • @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

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

    Thanks

    Siva

  • @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

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

  • 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

  • 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.

    Thanks!

    Preethi

  • 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??

    Thanks

    Preeti.

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

    Thanks,

    Girish V

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

Page 1 of 1 (15 items)
Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post