簡體   English   中英

在DatagridView c#中選中復選框刪除行

[英]Delete rows with checkedbox in DatagridView c#

我有一個數據網格,並添加了復選框以在需要時刪除一些行。 但是我使用的代碼不是100%正確,因為當我檢查1或2行,然后單擊我創建的刪除按鈕時,它會刪除我的所有行,而不僅是我選擇的行。

BindingSource myDataListBindingSource;
myDataListBindingSource = new BindingSource();

dataGrid_reservations.DataSource = myDataListBindingSource;     
gestion_hotel bdd1 = new gestion_hotel();
DataGridViewRow row = new DataGridViewRow();

for (int i = 0; i < dataGrid_reservations.Rows.Count; i++)
{
    row = dataGrid_reservations.Rows[i];
    if (Convert.ToBoolean(row.Cells[0].Value) == true)
    {
        int id = Convert.ToInt16(row.Cells[1].Value);
        dataGrid_reservations.Rows.Remove(row);
        i--;
    }
}

這是您檢查復選框是否被選中的方式

 foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
        if (chk.Value  == chk.TrueValue)
        {
            //it is checked
        }
        else
        {
            //it isnt checked
        }
    }

暫無
暫無

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

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