Friday, 12 February 2016

How to use Web Grid in ASP.NET MVC?

Web Grid is easy to use for those who are having complexity in various operations on ASP.NET Table or List View like Sorting, Paging etc.

Let use see how web grid works.

 Here we're having a view model that contains following data. We'll get the data and bind to Web Grid.





































  Now in controller create a ActionResult method in which we'll even call repository method for getting the data from database.



Repository Class Method:
























   
Now once you get the data as shown in Controller store the data in ViewBag and return to View.

In View get that list and bind to grid as shown.



















WebGrid object=new WebGrid(list);
                            
       where: list is Generic List. 

Now it's really interesting and easy to do Sorting, Paging in grid.

  1. Sorting is by default available in grid headers. If you want to disable you can use as:
WebGrid grid=new WebGrid(list,canSort:false);

      2. For paging :


WebGrid grid=new WebGrid(list,rowsPerPage:5);

      3. Display Column you want:

 WebGrid grid = new WebGrid(list,  columnNames: new[] { "E_Name", "E_Contact" });

      4. Change Column Header Name as shown in GetHtml():

@grid.GetHtml(columns:grid.Columns(grid.Column("E_Name","Name"),grid.Column("E_Contact","Contact Number")));



One can also perform many other operations this were the basic of them.
Thanks.

No comments:

Post a Comment

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....