如何处理Datagridview中按钮列中的点击事件? [英] How to handle click event in Button Column in Datagridview?

查看:105
本文介绍了如何处理Datagridview中按钮列中的点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 开发 Windows 应用程序.我使用 DataGridView 来显示数据.我在其中添加了一个按钮列.我想知道如何处理 DataGridView 中那个按钮的点击事件.

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.

推荐答案

您已经向 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:

首先,不要这样做:

我会避免这里的其他一些答案中的建议,甚至是 MSDN 上的文档 对列索引或列名称进行硬编码,以确定是否单击了按钮.click 事件注册整个网格,所以你需要以某种方式确定一个按钮被点击,但你不应该假设你的按钮位于特定的列名或索引中......有一种更简单的方法......

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...

另外,请注意您要处理的事件.同样,文档和许多示例都弄错了.大多数示例处理 CellClick 将触发的事件:

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.

...但也会在点击 row 标题时触发.这需要添加额外的代码来确定 e.RowIndex 值是否小于 0

...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 0

改为处理 CellContentClick 只发生:

Instead 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.

所以这是你应该做的:

首先,强制发送方输入DataGridView 以在设计时公开它的内部属性.您可以修改参数的类型,但这有时会使添加或删除处理程序变得棘手.

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.

接下来,要查看按钮是否被单击,只需检查以确保引发事件的列的类型为 DataGridViewButtonColumn.因为我们已经将发送者强制转换为类型 DataGridView,所以我们可以获取 Columns 集合并使用 e.ColumnIndex 选择当前列.然后检查该对象是否属于 DataGridViewButtonColumn 类型.

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 type DataGridView, we can get the Columns collection and select the current column using e.ColumnIndex. Then check if that object is of type DataGridViewButtonColumn.

当然,如果您需要区分每个网格的多个按钮,您可以根据列名或索引进行选择,但这不应该是您的第一项检查.始终确保首先单击按钮,然后适当地处理其他任何事情.在大多数情况下,每个网格只有一个按钮,您可以直接进入比赛.

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.

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


更新 1 - 自定义事件

如果您想获得一点乐趣,您可以添加您自己的事件,以便在 DataGrid 上单击按钮时引发.您不能将它添加到 DataGrid 本身,而不会弄乱继承等,但是您可以将自定义事件添加到您的表单并在适当的时候触发它.这是更多的代码,但好处是您已经将点击按钮时要执行的操作与如何确定按钮是否被点击分开.


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


更新 2 - 扩展网格

如果我们使用的网格可以为我们完成这些事情,那该多好.我们可以很容易地回答最初的问题:您已经向 DataGridView 添加了一个按钮,并且希望在单击它时运行一些代码.这是一种扩展 DataGridView 的方法.为每个库都交付一个自定义控件可能不值得麻烦,但至少它最大限度地重用了用于确定按钮是否被点击的代码.


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 the DataGridView. 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

就是这样.永远不要再碰它.确保您的 DataGrid 是 DataGridViewExt 类型,它应该与 DataGridView 完全相同.除了它还会引发一个额外的事件,您可以像这样处理:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆