Good day folks,

I was trying to do a simple project but somehow I'm stocked with it.

What I want to do is to get the index of a column in datagridview when a cell
is changed?

For example this is a dgv:

ID  |  UserID  |  Status  |

1     Ben        Offline
2     Joe        Offline
3     Ed         Offline

When I changed 'Ben' to 'Oliver', A messagebox will appear telling me that the
index of that column is 1

Any idea? Thanks

5 Years
Discussion Span
Last Post by Eternal Newbie

Came up with a bad idea, you might try this out:

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    End Sub

Hmmm. Thanks pal.. But I think, none of it will worked.

The value of a cell is changed programmatically, Using cellendedit is a good idea but
I still don't come up with what I wanted.


Then when changing your cell value you get it's index together? If you already had a code loop your datagridview through, get the ids and catch cells' index together with it.


@Eternal Newbie: first try out code for yourself, before posting it.
@GeekPlease: Did the link I gave not provide enough info?
Here is code from this link with addition:

' Forces the row to repaint itself when the user changes the  
' current cell. This is necessary to refresh the focus rectangle. 
Sub dataGridView1_CurrentCellChanged(ByVal sender As Object, _
    ByVal e As EventArgs) Handles dataGridView1.CurrentCellChanged

    If oldRowIndex <> -1 Then 
    End If
    oldRowIndex = Me.dataGridView1.CurrentCellAddress.Y
    MsgBox( Me.dataGridView1.CurrentCellAddress.X.ToString()) 'column index

End Sub 'dataGridView1_CurrentCellChanged

@ddanbe: Then I was wrong, sorry then, I did think that GeekPlease wants to return value from col "Id" in his table (which was different from the col index).
@GeekPlease: Changed my code, super fast and easy now:

    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    End Sub
Votes + Comments
That's the spirit!

Thanks guys, You're all great but I think I hadn't explained my problem correctly. My bad. BTW,

As I have said, The value is changed programmatically and the dgv readonly property is true.

Multiple value might exist in the record. When a value is changed 'programmatically' then the

messagebox will appear containing the column index of that data.


Hmmm then here I have 2 solution:
1. Saving an old datatable of datagridview data, edit your datagridview then loop through 2 tables to see the different, remove old datatable after save. In this case, you could avoid being full of MsgBoxes on screen.
2. Get your editing cells Selected = true together with your calculation. Each time a cell change, it will be select and then you can freely choose to use any solutions from ddanbe or from me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.