检测哪一列在 datagridview 中显示了编辑控件

3

我有一个DataGridView负责显示一些数据,其中两列允许用户使用组合框输入。

问题在于,一个列仅需要在其列表中显示预设值,但另一个列需要同时显示预设值并允许用户输入自己的值。

通过以下代码显示组合框的编辑控件来实现此目的:

Private Sub DGV_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing
    'todo: figure out which control is being edited (the reason or the action) and only allow the action column to allow user input
    If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
        Dim cb As ComboBox = e.Control
        cb.DropDownStyle = ComboBoxStyle.DropDown
    End If
End Sub

这允许在DGV的两个组合框中都输入用户输入,但我只想允许一个组合框进行用户输入。
有没有办法检测DGV中编辑控件来自哪一列,以便我不会为两个列运行此代码?
我是否错过了更好的方法?
1个回答

3

e.Control.EditingControlDataGridView.CurrentCell.ColumnIndex是什么?

或者只是DGV.CurrentCell.ColumnIndex?


工作得非常完美!谢谢!这个控件有很多东西,我很容易迷失。 - jrsconfitto
如果我想检查标题文本怎么办?例如... If DGV.Column.HeaderText="xyz" then - SamuraiJack
没事了,我找到解决方法了。 如果 DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).HeaderText = "输入数量" Then - SamuraiJack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接