Example E448
Visible to All Users

Grid View for ASP.NET Web Forms - How to dynamically switch the Grid's data source and recreate columns at runtime

This example demonstrates how to use the same grid to display different data sources (SqlDataSource instances).

Switch grid data sources

Grid columns are recreated when a new data source is assigned. Note that the data source is unconditionally assigned on each postback/callback in the ASPxGridView.Load event handler, while columns are recreated only after the data source is switched.

Files to Look At

Documentation

More Examples

Example Code

WebSite/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v13.2, Version=13.2.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <dx:aspxgridview id="ASPxGridView1" runat="server" AutoGenerateColumns="False" Width="910px" Settings-ShowGroupPanel="true" OnLoad="ASPxGridView1_Load"></dx:aspxgridview> &nbsp; <dx:ASPxRadioButton ID="ASPxRadioButton1" runat="server" AutoPostBack="True" Checked="True" GroupName="1" Text="Data Source #1"> </dx:ASPxRadioButton> <dx:ASPxRadioButton ID="ASPxRadioButton2" runat="server" AutoPostBack="True" GroupName="1" Text="Data Source #2"> </dx:ASPxRadioButton> </div> </form> </body> </html>
WebSite/Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using DevExpress.Web; public partial class _Default : System.Web.UI.Page { protected void ASPxGridView1_Load(object sender, EventArgs e) { // set DataSource ASPxGridView1.DataSource = GetCurrentTable(); // create columns, if necessary EnsureColumns(); // call DataBind() ASPxGridView1.DataBind(); } private DataTable GetCurrentTable() { if(ASPxRadioButton1.Checked) return GetTable1(); else return GetTable2(); } private DataTable GetTable1() { DataTable dt = (DataTable)Session["Table1"]; if(dt == null) { dt = new DataTable(); dt.Columns.Add("ID1"); dt.Columns.Add("Data1"); for(int i = 0; i < 5; i++) dt.Rows.Add(i, "row 1 " + i.ToString()); Session.Add("Table1", dt); } return dt; } private DataTable GetTable2() { DataTable dt = (DataTable)Session["Table2"]; if(dt == null) { dt = new DataTable(); dt.Columns.Add("ID2"); dt.Columns.Add("Data2"); for(int i = 0; i < 10; i++) dt.Rows.Add(i, "row 2 " + i.ToString()); Session.Add("Table2", dt); } return dt; } private void EnsureColumns() { if(ASPxGridView1.Columns.Count == 0) ReCreateColumns(); else { DataTable table = GetCurrentTable(); // if the grid has other columns than the assigned data source, // recreate columns if(ASPxGridView1.Columns[table.Columns[0].ColumnName] == null) ReCreateColumns(); } } private void ReCreateColumns() { ASPxGridView1.Columns.Clear(); DataTable table = GetCurrentTable(); foreach(DataColumn dataColumn in table.Columns) { GridViewDataTextColumn column = new GridViewDataTextColumn(); column.FieldName = dataColumn.ColumnName; // set additional column properties column.Caption = dataColumn.ColumnName; ASPxGridView1.Columns.Add(column); } } }

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.