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

Recommended Answers

All 8 Replies

You could use the CurrentCellAdress property. Click Here

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
        MsgBox(DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells("Text").Value.ToString())
    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 
        Me.dataGridView1.InvalidateRow(oldRowIndex)
    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
        MsgBox(DataGridView1.SelectedCells.Item(0).ColumnIndex)
    End Sub
commented: That's the spirit! +14

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.