【问题标题】:c#: Adding value to labelc#:为标签添加值
【发布时间】:2010-10-18 10:57:12
【问题描述】:

当我点击我的 datagridview 命令时如何运行?

          Int64 sum = 0;
        foreach (DataGridViewRow dr in dg_Cheque.Rows)
        {
            if (Convert.ToBoolean(dr.Cells["True_False"].Value) == true) //Cells[0] Because in cell 0th cell we have added checkbox
            {
                sum +=Convert.ToInt64(dr.Cells[0].Value.ToString());
            }
        }

        label1.Text = sum.ToString();

无论如何我都会写它?

【问题讨论】:

  • 你能澄清你的问题吗
  • 不要忘记在点击事件之后更新更新面板。
  • 不知道你在做什么,但你可以在 foreach 循环中删除 '== true',这不是必需的

标签: c# winforms events datagridview label


【解决方案1】:

这取决于您在 DataGridView 上单击的位置。如果您想在双击网格中的单元格时运行此命令,您可以使用CellContentDoubleClick 事件。

DataGridView can be found here 的完整事件列表

编辑:

您似乎想为 DataGrid 中的 CheckBox 捕获单击事件?

为了捕获 Checkbox changed 事件,您可以订阅 OnCellValueChanged 事件。在 EventArgs 中检查以查看已更改的列。如果它是您的复选框列,那么您可以运行您的命令。

类似这样的东西(未经测试):

private void DataGridView1_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
{

   if (e.ColumnIndex == 0 && e.RowIndex > -1) // Replace 0 with the checkbox col index
   {
         if ((bool)this.DataGridView1[e.ColumnIndex, e.RowIndex].Value == true)
         {
             // Checkbox is checked so call you command
         }
   }

}

【讨论】:

  • 这个事件不是答案
【解决方案2】:

有一个MouseClick 事件

【讨论】:

  • 这个事件不是答案
【解决方案3】:

所有事件都不响应

此列是一个复选框

我必须点击两次才能给出第一次点击的答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多