I have a WinForm in C#. One of the column of the DataGridView
is of type DataGridViewLinkColumn
. How do I handle the click event on each column ?
This code does not seem to work :
private void UserDataTable_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Code here
}
For instance, if I have 10 rows, whenever I click the content of each row corresponding to the column "DataGridViewLinkColumn
", I should be able to handle it.
Thanks
Why don't you use CellClick
event handler, you can refer to corresponding column of each row, e.RowIndex
by using e.ColumnIndex
, as shown below:
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
// here you can have column reference by using e.ColumnIndex
DataGridViewImageCell cell = (DataGridViewImageCell)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
// ... do something ...
}
Actually, I believe Kiran had it correct by using CellContentClick
. When you use this, it does not fire when a cell's empty space is clicked, only when its actual content is clicked. So if you have a DataGridViewLinkColumn
, it will fire when the link is clicked. If you have a DataGridViewTextBoxColumn
it will fire when the text in the cell is clicked. It will not fire if the empty space is clicked, or if the cell is empty it won't fire at all for that cell.
The CellClick
event fires whenever any part of a cell is clicked, including an empty one. @chessofnerd, I'm not sure why that wasn't working for you, but I've tested this to make sure, and at least for me it's working exactly as expected.
Kiran, that leads me to wonder why your CellContentClick
was not working in the first place. The first thing that comes to mind is to make certain that you're adding a new DataGridViewCellEventHandler
to the gridview's CellContentClick property. For example, if my gridview is titled gridVendorInfo I would need to use the following code first:
this.gridVendorInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.gridVendorInfo_CellContentClick);
And now I'd need to have that exact method in my code to actually catch it:
private void gridVendorInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string vendorName = "";
if (e.ColumnIndex == 0)
{
vendorName = gridVendorInfo.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
}
If you don't assign the gridview's CellContentClick event a new event handler and add the method spelled exactly the same, it won't fire. Hopefully that helps! It's much easier to just go to your form, click your gridview, go to the Events tab in the Properties window, find CellContentClick, and double click on the space to the right of it. VS will do all the work for you of creating the method and assigning a new eventhandler to the gridvew. Then you just need to go into the method and add some code and a breakpoint to see if it's firing, and it should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With