Example E2965
Visible to All Users

Grid View for ASP.NET Web Forms - How to bind the control created in design mode to different data sources at runtime

This example demonstrates how to bind an ASPxGridView with autogenerated columns to different data sources at runtime. The grid is created in design mode.

Switch Data Source at Runtime

The project contains an ASPxRadioButtonList control that allows users to switch between three data sources (SqlDataSource).

Note that the grid's EnableViewState property is set to false to avoid exceptions when binding the grid to another data source.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

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 autogenerated columns to different data sources at runtime. The grid is created at design mode.</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <td valign="top"> <dx:ASPxGridView ID="grid" runat="server" ClientInstanceName="grid" OnCustomCallback="grid_CustomCallback" OnDataBinding="grid_DataBinding" EnableViewState="false"> </dx:ASPxGridView> </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:AccessDataSource ID="dsProducts" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Products]"></asp:AccessDataSource> <asp:AccessDataSource ID="dsCategories" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"></asp:AccessDataSource> <asp:AccessDataSource ID="dsShippers" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [ShipperID], [CompanyName], [Phone] FROM [Shippers]"></asp:AccessDataSource> </form> </body> </html>
Solution/Default.aspx.cs(vb)
C#
using DevExpress.Utils; using DevExpress.Web; using System; using System.Linq; namespace Solution { public partial class Default : System.Web.UI.Page { enum DataSourceType { Products, Categories, Shippers } protected void Page_Init(object sender, EventArgs e) { if (!this.IsPostBack) Session.Clear(); grid.DataBind(); } 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); grid.Columns.Clear(); grid.AutoGenerateColumns = true; grid.KeyFieldName = String.Empty; grid.DataBind(); } private object 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; } } } }

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.