假设网格列的内置编辑器提供特定值的下拉列表,并且必须根据其他行的取值来筛选列表项。 例如,网格包含两个列:第一个列用于编辑 Country 字段,而第二个列提供选定国家的城市列表。 要只显示适当的取值,应该根据第一个列的取值来筛选第二个列的取值。
对于需要筛选下拉列表取值的列,可以使用 LookUpEdit 或 ComboBoxEdit 内置编辑器。 当使用 LookUpEdit 编辑器时,可以直接把在下拉列表中显示的项指派到编辑器的 DataSource 属性。 ComboBoxEdit 控件没有提供该属性。 应该通过把列表项添加到此编辑器的 Items 集合中,完成人工装载下拉列表。要筛选列的下拉列表值,则接管 ColumnView.ShownEditor 事件。 在此事件中,必须按要求筛选活动编辑器的下拉列表。 请参阅下面的示例代码。
要获得示例的完整源代码,请参阅下面的知识库文章 (需要因特网连接):
How to filter a second LookUp column based on a first LookUp column's valueC# | 复制代码 |
---|---|
using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid; private DataView clone = null; private void gridView1_ShownEditor(object sender, System.EventArgs e) { GridView view = sender as GridView; if (view.FocusedColumn.FieldName == "CityCode" && view.ActiveEditor is LookUpEdit) { Text = view.ActiveEditor.Parent.Name; DevExpress.XtraEditors.LookUpEdit edit; edit = (LookUpEdit)view.ActiveEditor; DataTable table = edit.Properties.LookUpData.DataSource as DataTable; clone = new DataView(table); DataRow row = view.GetDataRow(view.FocusedRowHandle); clone.RowFilter = "[CountryCode] = " + row["CountryCode"].ToString(); edit.Properties.LookUpData.DataSource = clone; } } private void gridView1_HiddenEditor(object sender, System.EventArgs e) { if (clone != null) { clone.Dispose(); clone = null; } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraEditors Imports DevExpress.XtraGrid.Views.Grid Private clone As DataView Private Sub GridView1_ShownEditor(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles GridView1.ShownEditor Dim view As GridView = CType(sender, GridView) If view.FocusedColumn.FieldName = "CityCode" AndAlso _ TypeOf view.ActiveEditor Is LookUpEdit Then Dim edit As LookUpEdit Dim table As DataTable Dim row As DataRow edit = CType(view.ActiveEditor, .LookUpEdit) table = CType(edit.Properties.DataSource, DataTable) clone = New DataView(table) row = view.GetDataRow(view.FocusedRowHandle) clone.RowFilter = "[CountryCode] = " + row("CountryCode").ToString() edit.Properties.DataSource = clone End If End Sub Private Sub GridView1_HiddenEditor(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles GridView1.HiddenEditor If Not clone Is Nothing Then clone.Dispose() clone = Nothing End If End Sub |