如何在DataGridView中找到CheckBox控件?

时间:2015-08-31 14:54:45

标签: c# winforms checkbox datagridview

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

1 个答案:

答案 0 :(得分:0)

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

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事件处理程序。