简体   繁体   English

如何处理DataGridViewLinkColumn的click事件

[英]How to handle the click event of DataGridViewLinkColumn

I have a WinForm in C#. 我在C#中有一个WinForm。 One of the column of the DataGridView is of type DataGridViewLinkColumn . 其中一个柱的DataGridView是类型的DataGridViewLinkColumn How do I handle the click event on each column ? 如何处理每列的click事件?

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. 例如,如果我有10行,每当我单击与“ DataGridViewLinkColumn ”列对应的每一行的内容时,我应该能够处理它。

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: 为什么不使用CellClick事件处理程序,可以通过使用e.ColumnIndex引用每行的相应列e.RowIndex ,如下所示:

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 . 实际上,我相信Kiran使用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. 因此,如果您有一个DataGridViewLinkColumn ,它将在单击链接时触发。 If you have a DataGridViewTextBoxColumn it will fire when the text in the cell is clicked. 如果您有一个DataGridViewTextBoxColumn ,它将在单击单元格中的文本时触发。 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. 单击单元格的任何部分(包括空单元格)时,将触发CellClick事件。 @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. @chessofnerd,我不确定为什么那不适合你,但我已经测试了这一点以确保,至少对我而言,它的工作完全符合预期。

Kiran, that leads me to wonder why your CellContentClick was not working in the first place. Kiran,这让我想知道为什么你的CellContentClick首先没有工作。 The first thing that comes to mind is to make certain that you're adding a new DataGridViewCellEventHandler to the gridview's CellContentClick property. 首先要想到的是确保您将新的DataGridViewCellEventHandler添加到gridview的CellContentClick属性中。 For example, if my gridview is titled gridVendorInfo I would need to use the following code first: 例如,如果我的gridview标题为gridVendorInfo,我需要首先使用以下代码:

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. 如果您没有为gridview的CellContentClick事件分配新的事件处理程序并添加拼写完全相同的方法,则不会触发。 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. 只需转到表单,单击gridview,转到Properties窗口中的Events选项卡,找到CellContentClick,然后双击它右侧的空格就容易多了。 VS will do all the work for you of creating the method and assigning a new eventhandler to the gridvew. VS将为您创建方法并为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. 然后你只需要进入方法并添加一些代码和一个断点来查看它是否正在触发,它应该是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM