How to handle the click event of DataGridViewLinkColumn
Asked Answered
A

2

17

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

Awake answered 22/10, 2012 at 12:23 Comment(0)
H
12

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 ...
}
Hilaire answered 22/10, 2012 at 12:26 Comment(4)
Thanks a lot. I had thought of a similar approach. However, I was wondering if I could make of DataGridViewLinkColumn to handle the click event.Awake
This handles a cell click. Ideally, I think we would want to attach to the link click. (How to do that without iterating over every link cell, I'm still trying to find out myself!)Ooze
I lied, the proposed solution does fire on link click and does not fire on cell click. This seems very inconsistent with what a cell click event should do, but hey, its what 99% of us wanted anyways! (and my apologies to the poor souls out there who really do want an event on cell click...) social.msdn.microsoft.com/Forums/windows/en-US/…Ooze
you should do DataGridViewImageCell cell = (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]) as DataGridViewImageCell; and then check that its not null, this event will fire for all cells, so you only want to handle cells with that typeOpinionative
S
8

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.

Spaulding answered 24/2, 2016 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.