Example E2968
Visible to All Users

Grid View for ASP.NET Web Forms - How to bind a grid created at runtime to different data sources

This example demonstrates how to add a Grid View control to a page at runtime and conditionally switch its data source.

Switch Data Sources

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

Documentation

More Examples

Example Code

Solution/Default.aspx
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>
Solution/Default.aspx.cs(vb)
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(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.