Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

like image 481
Ruben_PH Avatar asked Mar 14 '13 00:03

Ruben_PH


3 Answers

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

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
like image 148
Fabio Avatar answered Oct 29 '22 09:10

Fabio


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.

like image 33
Anbhu Avatar answered Oct 29 '22 11:10

Anbhu


Ah silly me,

DataGridView.CurrentCellAddress.X 'Column  
DataGridView.CurrentCellAddress.Y 'Row
like image 36
Ruben_PH Avatar answered Oct 29 '22 11:10

Ruben_PH