如何处理Datagridview按钮列中的点击事件? [英] How to handle click event in Button Column in Datagridview?
问题描述
DataGridView
来显示数据。我已经添加了一个按钮列。我想知道如何处理DataGridView中该按钮上的点击事件。您已经添加了一个按钮到您的 DataGridView
,并且您希望在点击时运行一些代码。
轻松简单 - 只需按照以下步骤操作:
不要:
首先,这是不:
我会避免在这里的一些其他答案中的建议,甚至由 MSDN中的文档硬编码列索引或列名,以确定是否点击了一个按钮。点击事件注册整个网格,所以不知何故你需要确定一个按钮被点击,但你不应该这样做,假设你的按钮存在一个特定的列名或索引...有一个更简单的方法...
此外,请小心您要处理的事件。同样,文档和许多例子都会出错。大多数示例处理 CellClick
$ b
当单元格的任何部分被点击时,
$ b
>
...但是,只要点击行标题,它也会触发。这就需要添加额外的代码来简单地确定 e.RowIndex
值是否小于0
a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx =noreferrer> CellContentClick
只发生:
当单元格内的内容被点击
< blockquote>
无论什么原因,列标题在单元格中也被视为内容,所以我们仍然需要检查以下内容。 / p>
Dos:
所以这里是你应该做的:
首先,发送者输入
DataGridView
,以在设计时公开其内部属性。您可以修改参数的类型,但有时可能会使添加或删除处理程序变得棘手。
接下来,要查看按钮是否被点击,请检查以确保提高事件的列的类型为
DataGridViewButtonColumn
。因为我们已经将发件人转换为键入DataGridView
,我们可以获取列
集合,并使用e.ColumnIndex
。然后检查该对象是否类型为DataGridViewButtonColumn
。
当然,如果您需要区分多个按钮网格,您可以根据列名或索引进行选择,但不应该是您的第一个检查。始终确保先按一下按钮,然后妥善处理任何其他内容。在大多数情况下,您每个网格只能有一个按钮,您可以直接跳到比赛。
将它们放在一起:
C#:
private void dataGridView1_CellContentClick (object sender,DataGridViewCellEventArgs e)
{
var senderGrid =(DataGridView)sender;
if(senderGrid.Columns [e.ColumnIndex]是DataGridViewButtonColumn&&
e.RowIndex> = 0)
{
// TODO - 按钮点击 - 执行代码
}
}
VB :
Private Sub DataGridView1_CellContentClick(sender As System.Object,e As DataGridViewCellEventArgs)_
处理DataGridView1.CellContentClick
Dim senderGrid = DirectCast(sender,DataGridView)
如果TypeOf senderGrid.Columns(e.ColumnIndex)是DataGridViewButtonColumn AndAlso
e.RowIndex> = 0然后
'TODO - 按钮点击 - 执行代码在这里
结束如果
结束Sub
更新1 - 自定义事件
如果你想要一点点有趣的是,只要按钮为c,就可以添加自己的事件舔在DataGrid上。您不能将其添加到DataGrid本身,而不会导致继承等等,但您可以在表单中添加自定义事件并在适当时触发它。这是一个更多的代码,但有一个好处是,当您点击按钮时,您已经分离出了您想要做的操作,以确定是否点击了按钮。
只需声明一个事件,适当时提高它并处理它。它将如下所示:
事件DataGridView1ButtonClick(作为DataGridView的发件人,作为DataGridViewCellEventArgs)
Private Sub DataGridView1_CellContentClick(sender As System.Object,e As DataGridViewCellEventArgs)处理DataGridView1.CellContentClick
Dim senderGrid = DirectCast(sender,DataGridView)
如果TypeOf senderGrid.Columns(e.ColumnIndex )DataGridViewButtonColumn AndAlso e.RowIndex> = 0然后
RaiseEvent DataGridView1ButtonClick(senderGrid,e)
End If
End Sub
Private Sub DataGridView1_ButtonClick(sender As DataGridView ,e As DataGridViewCellEventArgs)处理Me.DataGridView1ButtonClick
'TODO - 按钮点击 - 执行代码
End Sub
更新2 - 扩展网格
如果我们正在使用网格那只是为我们做了这些事情。我们可以很容易地回答初始问题:
您已经在DataGridView中添加了一个按钮,并且您希望在点击
时运行一些代码。这是一个扩展DataGridView
的方法。可能不值得为每个库提供自定义控件的麻烦,但至少它最大限度地重用用于确定按钮是否被点击的代码。
只需将其添加到您的程序集中:
公共类DataGridViewExt:继承DataGridView
事件CellButtonClick(发件人作为DataGridView,作为DataGridViewCellEventArgs)
私有子CellContentClicked(发件人作为System.Object,作为DataGridViewCellEventArgs)处理Me.CellContentClick
如果TypeOf Me.Columns(e .ColumnIndex)DataGridViewButtonColumn AndAlso e.RowIndex> = 0然后
RaiseEvent CellButtonClick(Me,e)
End If
End Sub
结束类
就是这样。永远不要再碰它。确保您的DataGrid类型为$ code> DataGridViewExt ,它们应该与DataGridView完全相同。除了它还会引发一个额外的事件,你可以这样处理:
Private Sub DataGridView1_ButtonClick发送方作为DataGridView,e作为DataGridViewCellEventArgs)_
处理DataGridView1.CellButtonClick
'TODO - 按钮单击 - 执行代码
End Sub
I am developing a windows application using C#. I am using
DataGridView
to display data. I have added a button column in that. I want to know how can I handle click event on that button in DataGridView.解决方案You've added a button to your
DataGridView
and you want to run some code when it's clicked.
Easy peasy - just follow these steps:Don'ts:
First, here's what NOT to do:
I would avoid the suggestions in some of the other answers here and even provided by the documentation at MSDN to hardcode the column index or column name in order to determine if a button was clicked. The click event registers for the entire grid, so somehow you need to determine that a button was clicked, but you should not do so by assuming that your button lives in a particular column name or index... there's an easier way...
Also, be careful which event you want to handle. Again, the documentation and many examples get this wrong. Most examples handle the
CellClick
event which will fire:when any part of a cell is clicked.
...but will also fire whenever the row header is clicked. This necessitates adding extra code simply to determine if the
e.RowIndex
value is less than 0Instead handle the
CellContentClick
which only occurs:when the content within a cell is clicked
For whatever reason, the column header is also considered 'content' within a cell, so we'll still have to check for that below.
Dos:
So here's what you should do:
First, cast the sender to type
DataGridView
to expose it's internal properties at design time. You can modify the type on the parameter, but that can sometimes make adding or removing handlers tricky.Next, to see if a button was clicked, just check to make sure that the column raising the event is of type
DataGridViewButtonColumn
. Because we already cast the sender to typeDataGridView
, we can get theColumns
collection and select the current column usinge.ColumnIndex
. Then check if that object is of typeDataGridViewButtonColumn
.Of course, if you need to distinguish between multiple buttons per grid, you can then select based on the column name or index, but that shouldn't be your first check. Always make sure a button was clicked first and then handle anything else appropriately. In most cases where you only have a single button per grid, you can jump right off to the races.
Putting it all together:
C#:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { var senderGrid = (DataGridView)sender; if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { //TODO - Button Clicked - Execute Code Here } }
VB:
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick Dim senderGrid = DirectCast(sender, DataGridView) If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then 'TODO - Button Clicked - Execute Code Here End If End Sub
Update 1 - Custom Event
If you wanted to have a little bit of fun, you can add your own event to be raised whenever a button is clicked on the DataGrid. You can't add it to the DataGrid itself, without getting messy with inheritance etc., but you can add a custom event to your form and fire it when appropriate. It's a little more code, but the upside is that you've separated out what you want to do when a button is clicked with how to determine if a button was clicked.
Just declare an event, raise it when appropriate, and handle it. It will look like this:
Event DataGridView1ButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) Private Sub DataGridView1_CellContentClick(sender As System.Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick Dim senderGrid = DirectCast(sender, DataGridView) If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then RaiseEvent DataGridView1ButtonClick(senderGrid, e) End If End Sub Private Sub DataGridView1_ButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) Handles Me.DataGridView1ButtonClick 'TODO - Button Clicked - Execute Code Here End Sub
Update 2 - Extended Grid
What would be great is if we were working with a grid that just did these things for us. We could answer the initial question easily:
you've added a button to your DataGridView and you want to run some code when it's clicked
. Here's an approach that extends theDataGridView
. It might not be worth the hassle of having to deliver a custom control with every library, but at least it maximally reuses the code used for determining if a button was clicked.Just add this to your assembly:
Public Class DataGridViewExt : Inherits DataGridView Event CellButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) Private Sub CellContentClicked(sender As System.Object, e As DataGridViewCellEventArgs) Handles Me.CellContentClick If TypeOf Me.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.RowIndex >= 0 Then RaiseEvent CellButtonClick(Me, e) End If End Sub End Class
That's it. Never touch it again. Make sure your DataGrid is of type
DataGridViewExt
which should work exactly the same as a DataGridView. Except it will also raise an extra event that you can handle like this:Private Sub DataGridView1_ButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) _ Handles DataGridView1.CellButtonClick 'TODO - Button Clicked - Execute Code Here End Sub
这篇关于如何处理Datagridview按钮列中的点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!