博客园  :: 首页  :: 联系 :: 管理

Grid Editing - Cascading Combo Boxes

Posted on 2010-12-09 16:31  sunrack  阅读(1033)  评论(0编辑  收藏  举报

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="CascadingComboBoxes.aspx.cs"
Inherits="GridEditing_CascadingComboBoxes" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="Server">
<script type="text/javascript">
// <![CDATA[
function OnCountryChanged(cmbCountry) {
grid.GetEditor("City").PerformCallback(cmbCountry.GetValue().toString());
}
// ]]>
</script>
<dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" DataSourceID="AccessDataSourceMain"
KeyFieldName="CustomerID" Width="100%" AutoGenerateColumns="False" OnCellEditorInitialize="grid_CellEditorInitialize">
<Settings ShowGroupPanel="True" />
<SettingsEditing Mode="Inline" />
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0">
<EditButton Visible="True">
</EditButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataComboBoxColumn FieldName="Country" VisibleIndex="1">
<PropertiesComboBox TextField="Country" ValueField="Country" EnableSynchronization="False"
IncrementalFilteringMode="StartsWith" DataSourceID="AccessDataSourceCountry">
<ClientSideEvents SelectedIndexChanged="function(s, e) { OnCountryChanged(s); }"></ClientSideEvents>
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn FieldName="City" VisibleIndex="2">
<PropertiesComboBox EnableSynchronization="False" IncrementalFilteringMode="StartsWith"
DropDownStyle="DropDown">
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataColumn VisibleIndex="3" FieldName="ContactName">
</dx:GridViewDataColumn>
<dx:GridViewDataColumn VisibleIndex="4" FieldName="CompanyName">
</dx:GridViewDataColumn>
</Columns>
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSourceMain" runat="server" DataFile="~/App_Data/nwind.mdb"
OnDeleting="AccessDataSource1_Modifying" OnInserting="AccessDataSource1_Modifying"
OnUpdating="AccessDataSource1_Modifying" SelectCommand="SELECT * FROM [Customers]"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [ContactName] = ?, [City] = ?, [Country] = ? WHERE [CustomerID] = ?">
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</UpdateParameters>
</asp:AccessDataSource>
<asp:AccessDataSource ID="AccessDataSourceCountry" runat="server" DataFile="~/App_Data/WorldCities.mdb"
SelectCommand="SELECT * FROM [Countries]"></asp:AccessDataSource>
<asp:AccessDataSource ID="AccessDataSourceCities" runat="server" DataFile="~/App_Data/WorldCities.mdb"
SelectCommand="SELECT c.City FROM [Cities] c, [Countries] cr WHERE (c.CountryId = cr.CountryId) AND (cr.Country = ?) order by c.City">
<SelectParameters>
<asp:Parameter Name="?" />
</SelectParameters>
</asp:AccessDataSource>
</asp:Content>

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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.ASPxCallbackPanel;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.Demos;
public partial class GridEditing_CascadingComboBoxes : Page {
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
grid.StartEdit(3);
}
}
protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
if(!grid.IsEditing || e.Column.FieldName != "City") return;
if(e.KeyValue == DBNull.Value || e.KeyValue == null) return;
object val = grid.GetRowValuesByKeyValue(e.KeyValue, "Country");
if(val == DBNull.Value) return;
string country = (string)val;
ASPxComboBox combo = e.Editor as ASPxComboBox;
FillCityCombo(combo, country);
combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
}
protected void FillCityCombo(ASPxComboBox cmb, string country) {
if (string.IsNullOrEmpty(country)) return;
List<string> cities = GetCities(country);
cmb.Items.Clear();
foreach (string city in cities)
cmb.Items.Add(city);
}
List<string> GetCities(string country) {
List<string> list = new List<string>();
AccessDataSourceCities.SelectParameters[0].DefaultValue = country;
DataView view = (DataView)AccessDataSourceCities.Select(DataSourceSelectArguments.Empty);
for(int i = 0; i < view.Count; i++) {
list.Add((string)view[i][0]);
}
return list;
}
protected void AccessDataSource1_Modifying(object sender, SqlDataSourceCommandEventArgs e) {
Utils.AssertNotReadOnly();
}
void cmbCity_OnCallback(object source, CallbackEventArgsBase e) {
FillCityCombo(source as ASPxComboBox, e.Parameter);
}
}

 

http://demos.devexpress.com/ASPxGridViewDemos/GridEditing/CascadingComboBoxes.aspx