How to: Bind the ASPxGridView to Data Created at Runtime (DataTable)

  • 2 minutes to read

This example demonstrates how you can programmatically create a DataTable and then bind a grid control to it.

The ASPxGridView control supports the caching mechanism (it is enabled by default). If you wish to use it and bind the grid at runtime, use the following approach. In this case, ASPxGridView will be re-populated only when it requires data. In other cases, the grid will take data from its own custom callback state.

See Also:

Knowledge Base Article: K18183: Why might paging (sorting, grouping, filtering) not work in the ASPxGridView?

Knowledge Base Article: K18567: ASPxGridView Row Cache


For a full example, refer to Bind a grid to a DataTable via code.

protected void Page_Load(object sender, EventArgs e) {
     if (!IsPostBack && !IsCallback) {
          //Create columns on the first load.
          GridViewDataColumn id = new GridViewDataColumn();
          id.FieldName = "id";
          GridViewDataColumn data = new GridViewDataColumn();
          data.FieldName = "data";
     //Bind the grid only once
     if (!IsPostBack)

protected void grid_DataBinding(object sender, EventArgs e) {
     // Assign the data source in grid_DataBinding
     grid.DataSource = GetTable();

DataTable GetTable() {
     //You can store a DataTable in the session state
     DataTable table = Session["Table"] as DataTable;
     if (table == null) {
          table = new DataTable();
          table.Columns.Add("id", typeof(int));
          table.Columns.Add("data", typeof(String));
          for (int n = 0; n < 100; n++) {
              table.Rows.Add(n, "row" + n.ToString());
          Session["Table"] = table;
     return table;