VB.NET DataGridView下拉框过滤及空值清除问题求助
DataGridView级联ComboBox列:实现误选内容一键清除
我之前参考《DataGridView Cascading/Dependent ComboBox Columns》一文,做了一个包含两个ComboBox列的DataGridView项目:其中第二列SOTTOFASE的选项会根据第一列FASE的选择动态过滤,实现了级联效果。但使用过程中发现,用户误选SOTTOFASE后没有便捷的清除方式,于是我针对性做了处理,下面是具体内容:
原级联ComboBox实现代码
先贴出我用来实现级联功能的核心代码:
- 第一列ComboBox:
FASE - 第二列ComboBox:
SOTTOFASE
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing Dim grid = DirectCast(sender, DataGridView) If (grid.CurrentCell.ColumnIndex = 1) Then 'State column Dim combo = DirectCast(e.Control, DataGridViewComboBoxEditingControl) If (grid.CurrentRow.Cells(0).Value IsNot DBNull.Value) Then Dim dvStates = New DataView(MondoDBDataSet1.Tables("SOTTOFASI"), $"n_fase = '{grid.CurrentRow.Cells(0).Value}'", "n_fase ASC", DataViewRowState.CurrentRows) combo.DataSource = dvStates combo.ValueMember = "n_sottofase" combo.DisplayMember = "sottofase" Else combo.DataSource = Nothing End If End If End Sub
(附一张展示操作流程3个步骤的图片:)
误选清除的解决方案
我通过监听DataGridView的KeyUp事件,实现了按下Delete键一键清除SOTTOFASE列选中值的功能,代码如下:
Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp If GetAsyncKeyState(Keys.Delete) Then Dim CellCombo As DataGridViewComboBoxCell If DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value <> -1 Then CellCombo = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1) CellCombo.Value = DBNull.Value End If End If End Sub
内容的提问来源于stack exchange,提问作者Simone
相关产品推荐
相关产品推荐

