How to get current column index when editing a cell in a DataGridView using VB.NET

I have a DataGridView with 5 columns.
Column cells 1 and 5 are ComboBoxes.
I have a function that runs whenever I change any of these ComboBoxes.

Now, for the function to work correctly, I need to get in which column the ComboBox that I edited works.

In a peculiar way, when I modify a ComboBox that belongs to column 1, Function 1 is executed.
When I change the ComboBox that belongs to column 5, Function 2 is executed.

+4
source share
3 answers

or

 DataGridView.CurrentCell.ColumnIndex 

Then, if you have predefined columns in the DataGridView (for example, the column name will be DataGridView_ComboBoxOne ) and you do not want the hardcode to compare the indices

You can use the following:

 Select case DataGridView.CurrentCell.ColumnIndex Case DataGridView_ComboBoxOne.Index Function1() Case DataGridView_ComboBoxTwo.Index Function2() Case Else 'Update of other columns End Select 
+5
source
 Dim columnIndex as Integer columnIndex = e.ColumnIndex 

This is a direct way to get the column index without using the current cell. Use it in DataGridView1_ColumnHeaderMouseClick.

+2
source

Ah stupid of me

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

Source: https://habr.com/ru/post/1469107/


All Articles