In this blog, today, I will show you how to write code in ASP.NET MVC for implementation of WebGrid Control using Static Data. In later sessions, I will show you the process of using GridView Dynamically, that means using SQL Server data source.
Steps for creating WebGrid
- First, create an application named WebgridOrGridview.

- Create a Class file in Models folder Employee.cs.Here, I have added some static data which can be shown in rows of WebGrid.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace WebgridOrGridview.Models {
- public class Employee {
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public double Salary {
- get;
- set;
- }
- public static List < Employee > GetList() {
- List < Employee > Employees = new List < Employee > {
- new Employee {
- FirstName = "Satyaprakash1", LastName = "Samantaray1", Salary = 45000
- },
- new Employee {
- FirstName = "Satyaprakash2", LastName = "Samantaray2", Salary = 25000
- },
- new Employee {
- FirstName = "Satyaprakash3", LastName = "Samantaray3", Salary = 25000
- },
- new Employee {
- FirstName = "Satyaprakash4", LastName = "Samantaray4", Salary = 35000
- },
- new Employee {
- FirstName = "Satyaprakash5", LastName = "Samantaray5", Salary = 65000
- },
- new Employee {
- FirstName = "Satyaprakash6", LastName = "Samantaray6", Salary = 75000
- },
- new Employee {
- FirstName = "Satyaprakash7", LastName = "Samantaray7", Salary = 85000
- },
- new Employee {
- FirstName = "Satyaprakash8", LastName = "Samantaray8", Salary = 95000
- },
- };
- return Employees;
- }
- }
- }

- Create a Controller named HomeController.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using WebgridOrGridview.Models;
- namespace WebgridOrGridview.Controllers {
- public class HomeController: Controller {
- public ActionResult show() {
- var empoyees = Employee.GetList(); //Reference of Model Class File.
- return View(empoyees);
- }
- }
- }
- Add a View in Views folder named show.cshtml.
- @ * @model IEnumerable < WebgridOrGridview.Models.Employee > * @
- @ {
- Layout = null;
- }
- @ {
- var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3);
- } < h2 style = "color:blue" > Web Grid In MVC4 < /h2> < style type = "text/css" >
- /*Here we will add css for style webgrid*/
- .webgrid - table {
- font - family: "Trebuchet MS", Arial, Helvetica, sans - serif;
- font - size: 1.2e m;
- width: 100 % ;
- display: table;
- border - collapse: separate;
- border: solid 1 px blue;
- background - color: white;
- }.webgrid - table td, th {
- border: 1 px solid blue;
- padding: 3 px 7 px 2 px;
- }.webgrid - header {
- background - color: yellow;
- color: red;
- padding - bottom: 4 px;
- padding - top: 5 px;
- text - align: left;
- }
- /*.webgrid-footer {
- }*/
- .webgrid - row - style {
- padding: 3 px 7 px 2 px;
- }.webgrid - alternating - row {
- background - color: pink;
- padding: 3 px 7 px 2 px;
- } /*for alteranating row style*/ < /style> < div id = "grid" > @grid.GetHtml(tableStyle: "webgrid-table", headerStyle: "webgrid-header",
- //footerStyle: "webgrid-footer",
- alternatingRowStyle: "webgrid-alternating-row", rowStyle: "webgrid-row-style", mode: WebGridPagerModes.All, firstText: "<< First", previousText: "< Prev", nextText: "Next >", lastText: "Last >>", //add next or prev link in webgrid
- columns: grid.Columns(grid.Column("FirstName"), grid.Column("LastName"), grid.Column("Salary")
- //grid.Column(header: "Action",format:@<text>@Html.ActionLink("Modify","") | @Html.ActionLink("Detail", "")| @Html.ActionLink("Remove", "")</text>) @*format: @<text>[email protected]</text>*@ @*Format of currency*@
- )) < /div>
- In show.chtml, add one Class that references WebGrid.
- @ {
- var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3);
- }
- //We can add Max No. of pages in webgrid when the page loads
- To get this WebGrid reference, we have to add some assembly / DLL reference in "References" folder i.e. System.Web.Helpers.dll.

- Also, we can check this WebGrid Reference mentioned in Show.Cshtml in other way. Right click on WebGrid in show.cshtml and "Go To Definition".
- Then, the Metadata definition file for this WebGrid will be shown.i.e. WebGrid[from metadata]
- #region Assembly System.Web.Helpers.dll, v2 .0 .0 .0
- // C:\Users\Satya\Desktop\Satyaa\Training Task\Mvc Apps\WebgridOrGridview\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\
- System.Web.Helpers.dll
- # endregion

















Nikul VyasPosted Jun 10, 2019, 5:54 PM
Hello Mr. Satyaprakash, this is a good article to follow, however, what if I don't want to be dependent on model. I have created a function that returns the list<string> type of data. The format of string is id=1; displayName=abc; age=22; where the data on left hand side is heading and right side Is values for the rows. Is there a way to bind the string list to WebGrid datasource? I have posted the link to the thread here: https://forums.asp.net/p/2156469/6265409.aspx?Loading+the+PowerShell+AD+Group+Details+from+PSObject+into+WebGrid+
Syed AliPosted Sep 13, 2018, 1:12 PM
Thank you for the article. I am getting the following message: Inheritance security rules violated by type: 'System.Web.Helpers.WebGridRenderer'. Derived types must either match the security accessibility of the base type or be less accessible.