【问题标题】:How to find CheckBox control in DataGridView?如何在 DataGridView 中找到 CheckBox 控件?
【发布时间】:2015-11-25 15:49:07
【问题描述】:

如何将datagridview(2)中的复选框项目添加到datagridview(1) 用于在 datagridview(1) 上的复选框(数据库)中显示数据

我的代码

DataTable a = tablebill();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    bool checkBoxValue = Convert.ToBoolean(row.Cells[0].Value);
    if (checkBoxValue == true)
    {
        a.Rows.Add(row.Cells["Products"].Value);
    }
    else { }
}
dataGridView1.DataSource = a;

【问题讨论】:

  • 根据您提供的代码 sn-p ,复选框单元格位于该行的第一个单元格中。是这样吗?
  • 是的,这是第一个,我想选中复选框以添加到 datagridview i.imgur.com/cPPQoMD.png?1 对不起语言。我在泰国学习

标签: c# winforms checkbox datagridview


【解决方案1】:

我假设您想在触发复选框单击事件时添加这些值。如果是这样,您可以尝试以下方法..

private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    //This will indicate the end of the cell edit (checkbox checked)
    if (e.ColumnIndex == dataGridView1.Columns[0].Index &&
        e.RowIndex != -1)
    {
        dataGridView1.EndEdit();
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridView1.Columns[0].Index && 
        e.RowIndex != -1)
    {
        //Handle your checkbox state change here
        DataTable a = tablebill();
        bool checkBoxValue = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
        if (checkBoxValue == true)
        {
            a.Rows.Add(dataGridView1.Rows[e.RowIndex].Cells["Products"].Value);
        }
        else { }
        dataGridView1.DataSource = a;
    }
}

PS。请记住正确添加您的 dataGridView1 事件处理程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多