【问题标题】:datagridview button column Winformsdatagridview 按钮栏 Winforms
【发布时间】:2012-01-18 11:32:19
【问题描述】:

我在DataGridView 上有一个按钮列,我正在尝试处理Button.Click 事件,但是当我单击一个按钮时没有任何反应。有什么帮助吗?

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show(e.ColumnIndex.ToString());
    if (e.ColumnIndex == 5)
    {
        MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
    }
}

【问题讨论】:

  • 嗨@pikk,你的意思是事件根本没有触发吗?
  • 没错,当我点击按钮时没有任何反应

标签: c# c#-4.0 datagridview


【解决方案1】:

我已经尝试过您的示例,并且效果很好。您真的将事件绑定到您的DataGridView 吗?

请检查<YourFormName>.Designer.cs 类中的InitializeComponent() 方法。 真的有吗

this.dataGridView1.CellClick += 
  new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

并且您没有在代码中的另一个位置删除处理程序?

DataGridViewButtonColumn MSDN 页面有这样的说法:

要响应用户按钮点击,请处理 DataGridView.CellClick 或 DataGridView.CellContentClick 事件。在事件处理程序中,您可以使用 DataGridViewCellEventArgs.ColumnIndex 属性来确定单击是否发生在按钮列中。您可以使用 DataGridViewCellEventArgs.RowIndex 属性来确定单击是否发生在按钮单元格中而不是列标题上。

【讨论】:

    【解决方案2】:

    让我得到/澄清您在 c# windows 窗体中的每一行 datagridview 上都有按钮。 因此,为了获得 datagridview 的按钮单击事件,我向您展示了一个示例 c# 代码,它对我来说很好用!下面给出的是我的 datagridview 按钮单击事件的 c# 代码,效果很好:

    private void dgTasks_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var button = (DataGridView)sender;
            if (button.Columns[6] is DataGridViewColumn && e.RowIndex >= 0)
            {
           MessageBox.Show("You have clicked Cancel button of a row of datagridview ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }
            }
    

    dgTasks 是我在 c# winforms 中的 datagridview 的名称。 button.Columns[6] 是我的 datagridview 的列号,其中我的按钮列名为:btnCancel,标题文本:CANCEL 和文本:取消。 这只是一个样本!希望你觉得它有用。!

    【讨论】:

      【解决方案3】:

      听起来您的活动没有正确注册。要检查,从设计器中突出显示DataGridView 控件并选择Properties,在Events 下检查CellClick 事件是否有条目,如果没有,应该有一个可用的下拉列表来列出您的事件 - 选择它应该可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 2011-12-16
        • 2022-12-30
        • 2010-09-16
        • 2021-05-03
        • 2022-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        相关资源
        最近更新 更多