簡體   English   中英

如何在DataGridView中找到CheckBox控件?

[英]How to find CheckBox control in 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;

我假設您想在觸發復選框單擊事件時添加這些值。 如果是這樣,您可以嘗試以下方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM