# Bind Grid to DataTable

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Product version</td>
  <td>2025.1.227</td>
 </tr>
</table>

## Description

How can I bind the Grid to a [`DataTable`](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=net-9.0)?

## Solution

To bind the Grid to a `DataTable`, you need to configure the Grid's DataSource to read from the `DataTable` and set up the necessary columns and properties.

> This approach is applicable when binding to any `dynamic` model.

The example relies on the following key steps:

1. Define the View's model as `System.Data.DataTable` and set up the Grid columns based on the `DataTable` columns. Also, using the same approach as for the columns, define the fields in the `Model` configuration of the DataSource.

   ```Razor HtmlHelper
   @using Kendo.Mvc.UI
   @model System.Data.DataTable

   @(Html.Kendo().Grid<dynamic>()
       .Name("Grid")
       .Columns(columns =>
       {
           foreach (System.Data.DataColumn column in Model.Columns)
           {
               var c = columns.Bound(column.ColumnName);
           }
           columns.Command(cmd=>cmd.Edit());
       })
       .Pageable()
       .Sortable()
       .Editable(ed=>ed.Mode(GridEditMode.PopUp))
       .Filterable()
       .Groupable()
       .Scrollable()
       .DataSource(dataSource => dataSource
           .Ajax()
           .Model(model =>
           {
               var id = Model.PrimaryKey[0].ColumnName;
               model.Id(id);
               foreach (System.Data.DataColumn column in Model.Columns)
               {
                   var field = model.Field(column.ColumnName, column.DataType);
                   if (column.ColumnName == id)
                   {
                       field.Editable(false);
                   }

               }
           })
           .Read(read => read.Action("Read", "Home"))
           .Update(update => update.Action("Update", "Home"))
       )
   )
   ```

2. Populate the `DataTable` with the respective data and pass it to the View.

   ```C#
   public ActionResult Index()
   {
       DataTable products = Products();

       return View(products);
   }

   private DataTable Products()
   {
       var connection = ConfigurationManager.ConnectionStrings["GridBindingDataTableEntities"].ConnectionString;
       using (var dataAdapter = new SqlDataAdapter("SELECT * from Products", connection))
       {
           var dataTable = new DataTable();

           dataAdapter.Fill(dataTable);
           dataAdapter.FillSchema(dataTable, SchemaType.Mapped);
           return dataTable;
       }
   }
   ```

## Sample Project

To review the complete project, refer to [ASP.NET MVC application](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridBindingDataTable) in the [UI for ASP.NET MVC Examples repository](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master).

The sample project resolves two main issues related to:

* [Editing](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base/grid-bind-to-datatable#editing)
* [Aggregates](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base/grid-bind-to-datatable#aggregates)

### Editing

The Grid does not have an instance to a model object and cannot infer field types. You need to provide the model field definitions yourself.

**Solution**

The metadata of the `DataTable` contains this information. Pull it into the model definition as illustrated in
[`Index.cshtml`](https://github.com/telerik/ui-for-aspnet-mvc-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridBindingDataTable/Views/Home/Index.cshtml).

### Aggregates

The aggregates suffer from a lack of type-information as well. The `ToDataSourceResult` method  does not have information about the field types and fails to compute the aggregates.

**Solution**

Provide type-information for the requested aggregates in the `DataSourceRequest` object. For more information, refer to [`HomeController.cs`](https://github.com/telerik/ui-for-aspnet-mvc-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridBindingDataTable/Controllers/HomeController.cs).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)
* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)
* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)
* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)
* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
