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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15399144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:44:01  来源:igfitidea点击:

How to get the current Column Index while editing a cell in DataGridView using VB.NET

vb.netdatagridview

提问by Ruben_PH

I have a DataGridViewwith 5 columns.
The Cells of Columns 1 and 5 are ComboBoxes.
I have a function that runs whenever I change any of these ComboBoxes.

我有一个DataGridView5 列。
第 1 列和第 5 列的单元格是组合框。
我有一个函数,每当我更改这些 ComboBox 中的任何一个时都会运行。

Now, for the function to run properly, I have to get which Column does the ComboBox that I have edited belongs to.

现在,为了让函数正常运行,我必须知道我编辑过的 ComboBox 属于哪个列。

Its like, when I change the ComboBox that belongs to Column 1, Function 1runs.
When I change the ComboBox that belongs to Column 5, Function 2runs.

就像,当我更改属于第 1 列的 ComboBox 时,Function 1它会运行。
当我更改属于第 5 列的 ComboBox 时,Function 2运行。

回答by Fabio

Or

或者

DataGridView.CurrentCell.ColumnIndex 

Then if your have a predefined columns in DataGridView(for example name of column will be DataGridView_ComboBoxOne) and don't want hardcode a compare of indexes

然后,如果您有一个预定义的列DataGridView(例如列的名称将是DataGridView_ComboBoxOne)并且不希望对索引进行硬编码

You can use like this:

你可以这样使用:

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

回答by Anbhu

Dim columnIndex as Integer

columnIndex = e.ColumnIndex

This is the straight way to get the column index without using current cell. Use it under DataGridView1_ColumnHeaderMouseClick.

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

回答by Ruben_PH

Ah silly me,

啊傻我

DataGridView.CurrentCellAddress.X 'Column  
DataGridView.CurrentCellAddress.Y 'Row