Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView button click event
 

DataGridView button click event

HI

I have a button in the first column of the datagridview. Whenever I click on the button in the datagridview, I want it to do get the id for that particular row. The ID is also in the first column of the datagridview. So, how do I get the value of the ID when the button is click? What even should I use as there is not button.click event.

Please help.

ahtan  Monday, August 27, 2007 7:57 PM

Handle the CellContentClick event of the DataGridView.

Evaluate DataGridView.Columns(e.ColumnIndex).Name to get the name of the column clicked

If it's your button column, then you can get the value of any cell in that row with

DataGridView.CurrentRow.Cells("ColumnNameHere").Value

Chris

Chris Langsenkamp  Monday, August 27, 2007 8:48 PM

Handle the CellContentClick event of the DataGridView.

Evaluate DataGridView.Columns(e.ColumnIndex).Name to get the name of the column clicked

If it's your button column, then you can get the value of any cell in that row with

DataGridView.CurrentRow.Cells("ColumnNameHere").Value

Chris

Chris Langsenkamp  Monday, August 27, 2007 8:48 PM
Thanks for the help.
ahtan  Tuesday, August 28, 2007 1:26 AM

Also, when doing column lookups, consider using an enumeration for your columns.

That way you can save the time on string comparisons by using:

protected enum { colName1, colName2, colName3}

public void xyzGrid_CellContentClick(.....)

{

...

l_DataGridView.CurrentRow.Cells[colName1]

...

}

Adam Lenda  Monday, March 17, 2008 7:25 PM
Or better, use the name variable affected to the datagridviews columns...
 column1.Name = "nColumnName";

And you can access with
l_DataGridView.CurrentRow.Cells["nColumnName"]

so, you can change the order of your columns without breaking your working code...
ScrumMaster  Thursday, December 04, 2008 9:33 PM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview Sorting
• Problem with comboBox and DataSource: event unexpected.
• Sorting unbound table in listbox with drag and drop
• DataGridView Getting Erroring Column in DataError event
• Dataset Performance/XBAP vs ASP.NET
• How to generate a table adapter using TypedDataSetGenerator?
• Focus in Form
• Strange combobox data integrity error on form closing??
• DataGrid.GridLineStyle = DataGridLineStyle.None doesn't work
• Connection Strings in Machine.Config and Table Adapter?