Question Datagridview Button Column Click Event

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

Hope someone can help :)

I have two columns in my datagridview with multiple rows.

Column 1 = Checkbox
Column 2 = Datagridview Button column

How can I trigger an event (example a message that appears) when the button is clicked in column 2 in a specific row? Not really sure how the clickevent for the Datagridview Button column works.

I cannot find any good examples on the internet.

Thanks
 
As I seem to post many times a day, the first place you should look is always the documentation. You're issue is with a DataGridViewButtonColumn, so you should read the documentation for that class. Said documentation contains this:
To respond to user button clicks, handle the DataGridView.CellClick or DataGridView.CellContentClick event. In the event handler, you can use the DataGridViewCellEventArgs.ColumnIndex property to determine whether the click occurred in the button column. You can use the DataGridViewCellEventArgs.RowIndex property to determine whether the click occurred in a button cell and not on the column header.
The documentation for the corresponding cell class contains similar information. If you know what type or member you're using, always read the documentation for that type or members when you have questions. More often than not, it will contain all you need, or at least give you enough information to go on with.
 
Hi

Thanks for your response.

I figured out that I can program the button using this code:

VB.NET:
Private Sub Table1DataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellClick
        ' Ignore clicks that are not on button cells. 
        If e.RowIndex < 0 OrElse Not e.ColumnIndex = _
            Table1DataGridView.Columns("Column2").Index Then Return

        MsgBox("Hello")

    End Sub

Problem is, all my buttons will show the same message in the same column. Any idea how I could program each button to have a unique message?

Sorry, I am still new at this :)
 
The answer is in your own code:
VB.NET:
        If [COLOR="Red"]e.RowIndex[/COLOR] < 0 OrElse Not e.ColumnIndex = _
            Table1DataGridView.Columns("Column2").Index Then Return
 
Back
Top