如何在DataGridView中找到CheckBox控件

本文关键字:CheckBox 控件 DataGridView | 更新日期: 2023-09-27 18:10:44

如何将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;

如何在DataGridView中找到CheckBox控件

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

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;
    }
}

p。请记住正确添加dataGridView1事件处理程序。