Skip to main content
All docs
V24.2

Bind Grid View to Data at Runtime

  • 3 minutes to read

This topic describes how to bind ASPxGridView to data at runtime.

ASPxGridView requires data on every page request to the server, because the control does not store its bound data in the ViewState (EnableViewState) between requests. Such behavior reduces the amount of data transferred between the client and the server. We recommend that you disable the EnableViewState property if you create the ASPxGridView control dynamically.

ASPxGridView automatically retrieves data from a data source if you assign it to the ASPxGridView.DataSourceID property. In this case, you do not need to rebind the control on each request.

Bind Before the ASPxGridView is Initialized

You can bind the ASPxGridView control to a DataTable object during the control’s initialization.

Assign your data source to the DataSource property and call the ASPxGridView.DataBind method within the ASPxGridView.Init or Page.Init event handler.

<dx:ASPxGridView runat="server" ID="ASPxGridView1" KeyFieldName="id" EnableRowsCache="false" ... >
    ...
</dx:ASPxGridView>
protected void Page_Init(object sender, EventArgs e) {
    ASPxGridView1.DataSource = GetTable();
    ASPxGridView1.DataBind();
}

DataTable GetTable() {
    DataTable 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());
    return table;
}

If grid data depends on data of other controls on the same page, handle the Page.Load event instead of the Page.Init event. An ASP.NET page loads the postback data and restores control values after the Page.Init event. Refer to the ASP.NET Page Life Cycle for more details.

Bind in the DataBinding Event Handler

This approach allows you to access values of other controls on the page and use these values to manage your data source (for example, select or filter data).

Assign your data source to the DataSource property within the control’s DataBinding event handler. Call the ASPxGridView.DataBind method in the Page.Load event handler. ASPxGridView raises the DataBinding event and gets data from your data source each time it requires data (see the Get Data from the ASPxGridView Row Cache section).

Note that a call to the DataBind method in the DataBinding event raises this event recursively.

View Example

<dx:ASPxGridView runat="server" ID="ASPxGridView1" KeyFieldName="id" EnableRowsCache="false" ... >
    ...
</dx:ASPxGridView>
protected void Page_Load(object sender, EventArgs e) {
    if(!IsPostBack) {
        ASPxGridView1.DataBind();
    }
}

protected void gridView_DataBinding(object sender, EventArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    string filterValue = (textBoxFilter as ASPxTextBox).Text;
    grid.DataSource = GetTable().Where(p => p.data.Contains(filterValue));

}

DataTable GetTable() {
    DataTable 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());
    return table;
}

Get Data from the ASPxGridView Row Cache

The ASPxGridView control supports the row cache.

The control stores information about the visible rows in its internal cache. It allows the grid to restore its data from the cache when visible row keys do not change (for example, delete rows). In this case, ASPxGridView does not raise the DataBinding event on a callback request. However, if you sort or filter data in the grid, the DataBinding event occurs because visible row indexes change. To disable the row cache, set the EnableRowsCache property to false (true is the default value) or call the Refresh method to update the grid data.

See the following articles for more information on how the row cache works:

GitHub Example

View Example: How to bind the control created in design mode to different data sources at runtime