简体   繁体   English

如何在DataGridView中找到CheckBox控件?

[英]How to find CheckBox control in DataGridView?

How to add item of checkbox in datagridview(2) to datagridview(1) for show data in checkbox(database) on datagridview(1) 如何将datagridview(2)中的复选框项添加到datagridview(1)中以显示datagridview(1)中的复选框(数据库)中的数据

My code 我的密码

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;

I'm assuming you want to add those values when a checkbox click event is triggered. 我假设您想在触发复选框单击事件时添加这些值。 If so, you could try the following.. 如果是这样,您可以尝试以下方法。

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. PS。 Remember to properly add your dataGridView1 event handlers. 请记住要正确添加dataGridView1事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM