This example demonstrates how to create templated combo box columns, add cascading combo box editors to the templates, and configure the grid's cell edit functionality in inline mode.
Overview
Add combo box columns to the grid, specify their EditItemTemplate properties, and add combo box editors to the templates.
ASPx<dx:GridViewDataComboBoxColumn FieldName="Category1ID" VisibleIndex="4">
<PropertiesComboBox DataSourceID="dsCategory1" TextField="Name" ValueField="ID" ValueType="System.Int32" />
<EditItemTemplate>
<dx:ASPxComboBox ID="Cat1" runat="server" DataSourceID="dsCategory1" TextField="Name" ValueField="ID"
Value='<%# Bind("Category1ID") %>' AutoPostBack="true" ValueType="System.Int32"
OnSelectedIndexChanged="Cat1_SelectedIndexChanged">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataComboBoxColumn>
For every template editor, handle its server-side SelectedIndexChanged event. In handlers, call the grid's FindEditRowCellTemplateControl method to access the subsequent editor and filter its data source based on the selected value of the primary editor.
C#protected void Cat1_SelectedIndexChanged(object sender, EventArgs e) {
ASPxComboBox combo1 = (ASPxComboBox)sender;
object oldCat1 = Session["Cat1ID"];
if(oldCat1 != null && oldCat1.Equals(combo1.Value)) return;
Session["Cat1ID"] = combo1.Value;
ASPxComboBox combo2 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category2ID"] as GridViewDataComboBoxColumn, "Cat2"));
combo2.Value = null;
Cat2_SelectedIndexChanged(combo2, EventArgs.Empty);
}
Wrap the grid with an update panel to avoid updating the entire page.
Files to Review
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- Data Editors for ASP.NET Web Forms - Custom callback based implementation of cascading comboboxes in inline mode
- Data Editors for ASP.NET Web Forms - How to edit hierarchical data using cascading comboboxes
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MultiCombo._Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:SqlDataSource ID="dsData" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Data] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Data] ([Title], [Description], [Category1ID], [Category2ID], [Category3ID], [Category4ID]) VALUES (@Title, @Description, @Category1ID, @Category2ID, @Category3ID, @Category4ID)" SelectCommand="SELECT * FROM [Data]"
UpdateCommand="UPDATE [Data] SET [Title] = @Title, [Description] = @Description, [Category1ID] = @Category1ID, [Category2ID] = @Category2ID, [Category3ID] = @Category3ID, [Category4ID] = @Category4ID WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Category1ID" Type="Int32" />
<asp:Parameter Name="Category2ID" Type="Int32" />
<asp:Parameter Name="Category3ID" Type="Int32" />
<asp:Parameter Name="Category4ID" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Category1ID" Type="Int32" />
<asp:Parameter Name="Category2ID" Type="Int32" />
<asp:Parameter Name="Category3ID" Type="Int32" />
<asp:Parameter Name="Category4ID" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [Category1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory2All" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
SelectCommand="SELECT ID, Name FROM Category2 ORDER BY Name"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory3All" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString4 %>"
SelectCommand="SELECT [ID], [Name] FROM [Category3] ORDER BY [Name]"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory4All" runat="server" EnableCaching="True" ConnectionString="<%$ ConnectionStrings:ConnectionString5 %>"
SelectCommand="SELECT [ID], [Name] FROM [Category4] ORDER BY [Name]"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString4 %>"
SelectCommand="SELECT [ID], [Name] FROM [Category3] WHERE ([Category2ID] = @Category2ID) ORDER BY [Name]">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="Category2ID" SessionField="Cat2ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
SelectCommand="SELECT ID, Name FROM Category2 WHERE (Category1ID = @Category1ID) ORDER BY Name">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="Category1ID" SessionField="Cat1ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCategory4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString5 %>"
SelectCommand="SELECT [ID], [Name] FROM [Category4] WHERE ([Category3ID] = @Category3ID) ORDER BY [Name]">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="Category3ID" SessionField="Cat3ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False"
DataSourceID="dsData" KeyFieldName="ID" OnStartRowEditing="grid_StartRowEditing"
OnHtmlRowCreated="grid_HtmlRowCreated" OnRowUpdating="grid_RowUpdating"
OnRowInserting="grid_RowInserting" EnableCallBacks="False">
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="true" ShowNewButton="true" ShowDeleteButton="true" />
<dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Title" VisibleIndex="2" />
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3" />
<dx:GridViewDataComboBoxColumn FieldName="Category1ID" VisibleIndex="4">
<PropertiesComboBox DataSourceID="dsCategory1" TextField="Name" ValueField="ID" ValueType="System.Int32" />
<EditItemTemplate>
<dx:ASPxComboBox ID="Cat1" runat="server"
DataSourceID="dsCategory1" TextField="Name" ValueField="ID" Value='<%# Bind("Category1ID") %>'
AutoPostBack="true" OnSelectedIndexChanged="Cat1_SelectedIndexChanged" ValueType="System.Int32">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn FieldName="Category2ID" VisibleIndex="5">
<PropertiesComboBox DataSourceID="dsCategory2All" TextField="Name" ValueField="ID"
ValueType="System.Int32" />
<EditItemTemplate>
<dx:ASPxComboBox ID="Cat2" runat="server"
DataSourceID="dsCategory2" TextField="Name" ValueField="ID"
AutoPostBack="true" OnSelectedIndexChanged="Cat2_SelectedIndexChanged" ValueType="System.Int32">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn FieldName="Category3ID" VisibleIndex="6">
<PropertiesComboBox DataSourceID="dsCategory3All" TextField="Name" ValueField="ID"
ValueType="System.Int32" />
<EditItemTemplate>
<dx:ASPxComboBox ID="Cat3" runat="server"
DataSourceID="dsCategory3" TextField="Name" ValueField="ID"
AutoPostBack="true" OnSelectedIndexChanged="Cat3_SelectedIndexChanged" ValueType="System.Int32">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn FieldName="Category4ID" VisibleIndex="7">
<PropertiesComboBox DataSourceID="dsCategory4All" TextField="Name" ValueField="ID"
ValueType="System.Int32" />
<EditItemTemplate>
<dx:ASPxComboBox ID="Cat4" runat="server"
DataSourceID="dsCategory4" TextField="Name" ValueField="ID"
AutoPostBack="true" ValueType="System.Int32">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataComboBoxColumn>
</Columns>
<SettingsEditing Mode="Inline" />
</dx:ASPxGridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
C#using System;
using DevExpress.Web;
using System.Collections.Specialized;
namespace MultiCombo {
public partial class _Default : System.Web.UI.Page {
protected void Cat1_SelectedIndexChanged(object sender, EventArgs e) {
ASPxComboBox combo1 = (ASPxComboBox)sender;
object oldCat1 = Session["Cat1ID"];
if(oldCat1 != null && oldCat1.Equals(combo1.Value)) return;
Session["Cat1ID"] = combo1.Value;
ASPxComboBox combo2 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category2ID"] as GridViewDataComboBoxColumn, "Cat2"));
combo2.Value = null;
Cat2_SelectedIndexChanged(combo2, EventArgs.Empty);
}
protected void Cat2_SelectedIndexChanged(object sender, EventArgs e) {
ASPxComboBox combo2 = (ASPxComboBox)sender;
object oldCat2 = Session["Cat2ID"];
if(oldCat2 != null && oldCat2.Equals(combo2.Value)) return;
Session["Cat2ID"] = combo2.Value;
ASPxComboBox combo3 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category3ID"] as GridViewDataComboBoxColumn, "Cat3"));
combo3.Value = null;
Cat3_SelectedIndexChanged(combo3, EventArgs.Empty);
}
protected void Cat3_SelectedIndexChanged(object sender, EventArgs e) {
ASPxComboBox combo3 = (ASPxComboBox)sender;
object oldCat3 = Session["Cat3ID"];
if(oldCat3 != null && oldCat3.Equals(combo3.Value)) return;
Session["Cat3ID"] = combo3.Value;
ASPxComboBox combo4 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category4ID"] as GridViewDataComboBoxColumn, "Cat4"));
combo4.Value = null;
}
protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e) {
if((e.RowType == GridViewRowType.InlineEdit)) {
ASPxComboBox combo2 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category2ID"] as GridViewDataComboBoxColumn, "Cat2"));
if(Request.Params[combo2.UniqueID] != null) return;
if(!grid.IsNewRowEditing) {
object[] vals = (object[])grid.GetRowValues(grid.EditingRowVisibleIndex,
new string[] { "Category2ID", "Category3ID", "Category4ID" });
ASPxComboBox combo4 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category4ID"] as GridViewDataComboBoxColumn, "Cat4"));
ASPxComboBox combo3 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category3ID"] as GridViewDataComboBoxColumn, "Cat3"));
combo2.Value = vals[0];
combo3.Value = vals[1];
combo4.Value = vals[2];
}
}
}
protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e) {
object[] vals = (object[])grid.GetRowValuesByKeyValue(e.EditingKeyValue,
new string[] { "Category1ID", "Category2ID", "Category3ID" });
Session["Cat1ID"] = vals[0];
Session["Cat2ID"] = vals[1];
Session["Cat3ID"] = vals[2];
}
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
SaveOrInsertData(e.NewValues);
}
private void SaveOrInsertData(OrderedDictionary NewValues) {
object cat1 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category1ID"] as GridViewDataComboBoxColumn, "Cat1")).Value;
NewValues["Category1ID"] = cat1;
object cat2 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category2ID"] as GridViewDataComboBoxColumn, "Cat2")).Value;
NewValues["Category2ID"] = cat2;
object cat3 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category3ID"] as GridViewDataComboBoxColumn, "Cat3")).Value;
NewValues["Category3ID"] = cat3;
object cat4 = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(
grid.Columns["Category4ID"] as GridViewDataComboBoxColumn, "Cat4")).Value;
NewValues["Category4ID"] = cat4;
}
protected void grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
SaveOrInsertData(e.NewValues);
}
}
}