add code to the click event of datagrid button in vb.net

taz8020
taz8020 used Ask the Experts™
on
Hi I have a button on a datagrid but how do you add code to the click event of that button. I want it so that when someone clicks the button in the data grid it adds 2 columns values togeather?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Craig WagnerSoftware Architect

Commented:
WinForms or ASP.NET?

Author

Commented:
sorry windows app
Software Architect
Commented:
I'm making a couple of assumptions here:

1. When you said datagrid you actually meant datagridview.

2. When you said you had a button on the datagrid, it's actually a datagridviewbuttoncolumn object.

If those assumptions are valid, then you need to use the CellClick event and check the row and column indexes to see if the user clicked the button column and not the row header.

You'll obviously need to change the ColumnIndex value to whatever column contains your button. In my example it was 5. Don't forget, column indexes start at 0.
private void dataGridView1_CellClick( object sender, DataGridViewCellEventArgs e )
{
    if( e.ColumnIndex == 5 && e.RowIndex > -1 )
    {
        MessageBox.Show( "You clicked the button!" );
    }
}

Open in new window

Author

Commented:
Thanks Craig
So simple yet wasted 3hrs looking for an answer.
I have a lot to learn

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial