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

查看:667
本文介绍了如何处理点击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 ,你想运行一些code时,它的点击。
易peasy - 只需按以下步骤操作:

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:

首先,这里是不会做的:

我会避免在某些位置,甚至其他的答案由<所提供的建议href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn.aspx">documentation在MSDN 的为C列索引或列名硬$ C $,以确定是否一个按钮被点击。单击事件注册为整个电网,所以不知为何,你需要确定一个按钮被点击,但是你不应该假设你的按钮,生活在一个特定的列名或索引...有一个更简单的方法做到这一点?

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

另外,要小心你要处理的事件。同样,文档和许多例子得到这个错误。大多数例子办理<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx"><$c$c>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.

...而且将火每当的头被点击。这就需要增加额外的code简单地确定 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

而不是处理<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx"><$c$c>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.

因此​​,这里是你应该做的:

So here's what you should do:

首先,铸造的发送者键入的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.

接下来,看看一个按钮被点击,只是检查,以确保该列引发事件的类型是<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn.aspx"><$c$c>DataGridViewButtonColumn.因为我们已经投了发送者键入的DataGridView ,我们可以得到收集和选择当前列使用 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.

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


更新1 - 自定义事件

如果你想有一个有趣的一点,你可以添加你自己的事件,只要一个按钮被点击DataGrid中得到提升。你可以把它添加到DataGrid本身,没有得到凌乱与传承等,但你可以添加自定义事件到窗体,并启动它在适当的时候。这是一个多一点code,但好处是,你要当点击与如何确定一个按钮被点击一个按钮,做你已经分离出来。


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,你想运行一些code时,它的点击。下面是扩展了的DataGridView 的方法。它可能不是值得拥有,提供每库中的自定义控件的麻烦,但至少它最大限度地重用用于如果一个按钮被点击确定code。


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

就是这样。再也不会去碰它。请确保您的数据网格的类型是 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天全站免登陆