In this article I will explain with an example, how to implement WebGrid with dynamic Columns in ASP.Net MVC Razor.
The list of Column names will be extracted from the Entity model using Reflection and will be used to create a List collection of WebGridColumn class.
The List collection of WebGridColumn class will be set in ViewBag object which will be later used in View to populate WebGrid with dynamic Columns in ASP.Net MVC Razor.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, first the list of Column names will be extracted from the Entity model using Reflection and will be used to create a List collection of WebGridColumn class.
The List collection of WebGridColumn class will be set in ViewBag object which will be later used in View to populate WebGrid with dynamic Columns in ASP.Net MVC Razor.
Finally, the Customer records are fetched using Entity Framework and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//Fetch the names of Columns from Entity model.
string[] columnNames = typeof(Customer).GetProperties().Select(property => property.Name).ToArray();
//Add Dynamic Columns to List collection.
List<WebGridColumn> columns = new List<WebGridColumn>();
columns.Add(new WebGridColumn() { ColumnName = columnNames[0], Header = columnNames[0] }); //CustomerID
columns.Add(new WebGridColumn() { ColumnName = columnNames[2], Header = columnNames[2] }); //ContactName
columns.Add(new WebGridColumn() { ColumnName = columnNames[5], Header = columnNames[5] }); //City
columns.Add(new WebGridColumn() { ColumnName = columnNames[8], Header = columnNames[8] }); //Country
//Assign the List of Dynamic WebGrid Columns to ViewBag.
ViewBag.Columns = columns;
NorthwindEntities entities = new NorthwindEntities();
return View(entities.Customers.ToList());
}
}
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
For displaying the records, the WebGrid is rendered using GetHtml function which renders the WebGrid using Model.
The ViewBag object containing the List of dynamic WebGrid Columns is assigned to the Columns property of the WebGrid.
@model IEnumerable<Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, canPage: true);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.Grid
{
border: 1px solid #ccc;
border-collapse: collapse;
}
.Grid th
{
background-color: #F7F7F7;
font-weight: bold;
}
.Grid th, .Grid td
{
padding: 5px;
border: 1px solid #ccc;
}
.Grid, .Grid table td
{
border: 0px solid #ccc;
}
.Grid th a, .Grid th a:visited
{
color: #333;
}
</style>
</head>
<body>
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: ViewBag.Columns)
</body>
</html>
Screenshot
Downloads