This example demonstrates how to add a Grid View control to a page at runtime and conditionally switch its data source.
Implementation Details
The example application's Default
page contains an ASPxRadioButtonList and three pre-configured data sources (SqlDataSource). A Grid View control is added to the page in code and bound to the data source that you select from the Radio Button List.
The grid's view state is enabled in this example. Because of this, when the grid is added to the page, it restores its columns from the view state. To generate the columns from scratch when the data source has been changed, clear the ASPxGridView.Columns collection before you call the ASPxGridView.DataBind method.
Files to Review
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- Grid View for ASP.NET Web Forms - How to dynamically switch the Grid's data source and recreate columns at runtime
- Grid View for ASP.NET Web Forms - How to create columns and bind the control to different data sources at runtime
- Grid View for ASP.NET Web Forms - How to bind the control created in design mode to different data sources at runtime
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Solution.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to bind ASPxGridView with autocreated columns to different data sources at runtime. The grid is created in runtime mode</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td valign="top">
<asp:PlaceHolder ID="phGrid" runat="server"></asp:PlaceHolder>
</td>
<td valign="top">
<dx:ASPxRadioButtonList ID="rblDatasources" runat="server">
<ClientSideEvents SelectedIndexChanged="function(s, e) { grid.PerformCallback(s.GetSelectedIndex()); }" />
<Items>
<dx:ListEditItem Selected="True" Text="Products" />
<dx:ListEditItem Text="Categories" />
<dx:ListEditItem Text="Shippers" />
</Items>
</dx:ASPxRadioButtonList>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsProducts" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Products]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategories" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsShippers" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ShipperID], [CompanyName], [Phone] FROM [Shippers]">
</asp:SqlDataSource>
</form>
</body>
</html>
C#using DevExpress.Web;
using System;
using System.Web.UI.WebControls;
namespace Solution {
public partial class Default : System.Web.UI.Page {
enum DataSourceType {
Products,
Categories,
Shippers
}
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack)
Session.Clear();
CreateGrid();
}
protected void grid_DataBinding(object sender, EventArgs e) {
(sender as ASPxGridView).DataSource = GetDataSource();
}
protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) {
Session["selectedDataSource"] = Int32.Parse(e.Parameters);
ASPxGridView grid = sender as ASPxGridView;
grid.Columns.Clear();
grid.AutoGenerateColumns = true;
grid.DataBind();
}
private SqlDataSource GetDataSource() {
object o = Session["selectedDataSource"];
DataSourceType dsType = DataSourceType.Products;
if (o != null)
dsType = (DataSourceType)o;
switch (dsType) {
case DataSourceType.Categories:
return dsCategories;
case DataSourceType.Shippers:
return dsShippers;
default:
return dsProducts;
}
}
private void CreateGrid() {
ASPxGridView grid = new ASPxGridView();
grid.ID = "grid";
grid.DataBinding += grid_DataBinding;
grid.CustomCallback += grid_CustomCallback;
phGrid.Controls.Add(grid);
grid.Columns.Clear();
grid.AutoGenerateColumns = true;
grid.ClientInstanceName = "grid";
grid.DataBind();
}
}
}