如何在使用VB.NET编辑DataGridView中的单元格时获取当前列索引

5

我有一个包含5列的DataGridView
第1列和第5列的单元格是下拉框。
每当我更改这些下拉框时,都会运行一个函数。

现在,为了使函数正常运行,我必须确定我编辑的下拉框属于哪一列。

就像这样,当我更改属于第1列的下拉框时,函数1运行。
当我更改属于第5列的下拉框时,函数2运行。

3个回答

5

或者

DataGridView.CurrentCell.ColumnIndex 

如果你在 DataGridView 中有预定义的列(例如列名为 DataGridView_ComboBoxOne),并且不想硬编码索引比较,可以使用以下方法:

Select case DataGridView.CurrentCell.ColumnIndex 
    Case DataGridView_ComboBoxOne.Index
        Function1()
    Case DataGridView_ComboBoxTwo.Index
        Function2()
    Case Else
        'Update of other columns
End Select

"CurrentCell.ColumnIndex"和"CurrentCellAddress.Y"的作用相同。 但我会使用"CurrentCell.ColumnIndex",因为它更易读。 我还测试了您的其他代码,也很好用。谢谢。 - Ruben_PH

4
Dim columnIndex as Integer

columnIndex = e.ColumnIndex

这是一种不使用当前单元格获取列索引的直接方法。在DataGridView1_ColumnHeaderMouseClick下使用它。


1
啊,我太傻了。
DataGridView.CurrentCellAddress.X 'Column  
DataGridView.CurrentCellAddress.Y 'Row

真的,很棒又新鮮的資訊,謝謝!^_^ - MAMPRO

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