【问题标题】:How to know a specific checkbox inside datagridview is checked or not?如何知道datagridview中的特定复选框是否被选中?
【发布时间】:2013-08-26 08:25:51
【问题描述】:

我有一个 gridview,它有 2 列,一个是文本框列,另一个是复选框列,如何知道选中了哪个复选框。

如图所示,假设任何一个复选框被选中,我想显示该复选框对应的文本框值。

谁能帮助我?我尝试了下面的代码,但我面临的问题是,一旦我点击下一个复选框,就会显示值,然后显示之前选中的复选框值..

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

  void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {  
        object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
         dataGridView1_CurrentCellDirtyStateChanged(sender, e);

        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }

【问题讨论】:

  • 你试图做什么?如果您尝试从互联网上搜索,我担心在这里写问题的时间会相同:datagridview.CellValueChanged eventdatagridview.CellEndEdit event
  • 只看给定单元格的值,记住,对于复选框,只有两个可能的值:真或假。
  • @varocarbas 我同意您的评论,但由于最初未选中复选框,因此检查真假它每次都返回假
  • ?!如果所有复选框都未选中,那么您每次都会得到错误(您期望什么?C# 对您撒谎?哈哈)。这就是重点:知道它们是未选中(= false)还是选中(= true)。如果你想改变它的行为,你可以遵循相同的规则,即:Cell.Value = True(选中它)和 Cell.Value = False(取消选中它)。

标签: c# winforms checkbox datagridview datagridviewcheckboxcell


【解决方案1】:
 private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }

这些下面的链接帮助我理解了 cellvalue_changed 和 cell_content_click 的概念。 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx

在这些链接的帮助下,我终于找到了解决问题的方法

【讨论】:

  • 行间阅读... :)
【解决方案2】:

就这么简单

//replace the row number and column name with your own 
if ((bool)dataGridView1.Rows[0].Cells["Column1"].Value)
 {
       //do your work
 }

【讨论】:

  • 它给出 nullReference 异常说“对象未设置为对象的引用”
  • 你改过datagridview的名字了吗?行号?和列名?如果是,您指定的行是否实际上包含记录?你初始化你的数据网格了吗?有很多事情可能出错。显示您的代码。
【解决方案3】:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex > -1 && e.ColumnIndex > -1)
    label1.Text = dataGridView1.Rows[e.RowIndex].Cells["Col1"].Value.ToString();
}

【讨论】:

  • 我想要 windows 窗体解决方案而不是 asp.net 解决方案...以及为什么我需要按钮 ..我只想在单击复选框时显示与该复选框对应的文本。
【解决方案4】:
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == *someIndex*)
    {
        DataGridViewCheckBoxCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
        if (cell != null)
        {
            if (cell.EditingCellValueChanged)
            {
                //CheckBox has been clicked
            }

            //here how to get the checkBoxCell value
            var cellChecked = cell.EditingCellFormattedValue;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多