In this article I will explain with an example, how to bind (populate) WebGrid using dynamic DataTable (DataSet) in ASP.Net MVC Razor.
Inside the Controller, first a DataTable object is created with some dynamic Columns and then records are added to the DataTable.
Finally the DataTable is added to a DataSet which is later used to populate the WebGrid in ASP.Net MVC Razor.
You will need to import the following namespace.
using System.Data;
The Controller consists of the Index Action method. Inside this Action method, first DataTable object is created with some dynamic Columns.
Then records are added to the DataTable and the DataTable is added to a DataSet.
Finally the DataSet is returned to the View.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        return View(ds);
Inside the View, the DataSet is declared as Model for the View.
The WebGrid is initialized with the Dynamic Anonymous Type collection by making use of LINQ i.e. the records from the DataTable are converted into Dynamic Anonymous Type collection and are assigned to the WebGrid.
The WebGrid is created using the GetHtml method with the following parameters.
HtmlAttributes – It is used to set the HTML attributes to the HTML Table generated by WebGrid such as ID, Name, Class, etc.
Columns – It is used to specify the columns to be displayed in WebGrid and also allows to set specific Header Text for the columns.
@using System.Data
@using System.Linq
@model DataSet
    Layout = null;
    WebGrid webGrid = new WebGrid(source: (from p in Model.Tables[0].AsEnumerable()
                                            select new
                                                CustomerId = p.Field<int>("CustomerId"),
                                                Name = p.Field<string>("Name"),
                                                Country = p.Field<string>("Country")
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <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;
    htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
    columns: webGrid.Columns(
                webGrid.Column("CustomerId", "Customer Id"),
                webGrid.Column("Name", "Name"),
                webGrid.Column("Country", "Country")))
Populate (Bind) WebGrid using dynamic DataTable (DataSet) in ASP.Net MVC