C# 如何处理DataGridViewLinkColumn的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13011407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle the click event of DataGridViewLinkColumn
提问by Kiran
I have a WinForm in C#. One of the column of the DataGridViewis of type DataGridViewLinkColumn. How do I handle the click event on each column ?
我在 C# 中有一个 WinForm。的列之一DataGridView是类型DataGridViewLinkColumn。如何处理每列上的点击事件?
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
谢谢
采纳答案by Furqan Safdar
Why don't you use CellClickevent handler, you can refer to corresponding column of each row, e.RowIndexby using e.ColumnIndex, as shown below:
为什么不使用CellClick事件处理程序,可以e.RowIndex通过使用来引用每一行对应的列e.ColumnIndex,如下图:
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 ...
}
回答by zpert
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 DataGridViewTextBoxColumnit 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.
实际上,我相信 Kiran 通过使用CellContentClick. 当您使用它时,它不会在单击单元格的空白区域时触发,只有在单击其实际内容时才会触发。所以如果你有一个DataGridViewLinkColumn,它会在点击链接时触发。如果你有一个DataGridViewTextBoxColumn它会在单元格中的文本被点击时触发。如果单击空白区域,它将不会触发,或者如果单元格为空,则根本不会为该单元格触发。
The CellClickevent 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.
CellClick每当单击单元格的任何部分(包括空的部分)时,都会触发该事件。@chessofnerd,我不确定为什么这对您不起作用,但我已经对此进行了测试以确保,至少对我来说它完全按预期工作。
Kiran, that leads me to wonder why your CellContentClickwas not working in the first place. The first thing that comes to mind is to make certain that you're adding a new DataGridViewCellEventHandlerto the gridview's CellContentClick property. For example, if my gridview is titled gridVendorInfo I would need to use the following code first:
Kiran,这让我想知道为什么你一CellContentClick开始没有工作。首先想到的是确保您正在向DataGridViewCellEventHandlergridview 的 CellContentClick 属性中添加一个新属性。例如,如果我的 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. 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.
如果您没有为 gridview 的 CellContentClick 事件分配一个新的事件处理程序并添加拼写完全相同的方法,它就不会触发。希望这有帮助!只需转到您的表单,单击您的 gridview,转到“属性”窗口中的“事件”选项卡,找到 CellContentClick,然后双击它右侧的空间,就容易多了。VS 将为您完成创建方法并将新事件处理程序分配给 gridvew 的所有工作。然后你只需要进入该方法并添加一些代码和一个断点,看看它是否正在触发,它应该是。

