4

我目前正在使用此代码:

foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
    dataGridView1.Rows.RemoveAt(item.Index);
}

我在第一列下有复选标记,但是使用此代码,它只会被选中。我如何让 Selected CheckBoxes与行一起删除?

4

5 回答 5

6

你想要类似的东西

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (Convert.ToBoolean(dataGridView1.Rows[i]
                          .Cells[yourCheckBoxColIndex].Value) == true)
    {
        dataGridView1.Rows.RemoveAt(i); 
    }
}

我希望这有帮助。

于 2012-07-04T08:04:03.683 回答
1

尝试这样的事情:

foreach(DataGridViewRow row in this.dataGridView1.Rows)
{
    var checked = Convert.ToBoolean(row.Cells[0].Value); // Assuming the first column contains the Checkbox
    if(checked)
        dataGridView1.Rows.RemoveAt(row.Index);
}
于 2012-07-04T07:58:56.807 回答
1

它可能是这样的......这是列表视图的一个例子,但是这个概念几乎就在那里。循环遍历该项目并找到复选框 id 并删除选中的那些。希望这可以帮助。

public void btnDeleteClick(object sender, EventArgs e)
    {
        // Iterate through the ListViewItem
        foreach (ListViewItem row in ListView1.Items)
        {
            // Access the CheckBox
            CheckBox cb = (CheckBox)row.FindControl("cbxID");
            if (cb != null && cb.Checked)
            {
                // ListView1.DataKeys[item.DisplayIndex].Values[0].ToString()
                try
                {

                }
                catch (Exception err)
                {

                }
            }
        }
    }
于 2012-07-04T08:02:30.397 回答
0

试试这个:

if (dgv.SelectedRows.Count>0)
        {
            dgv.Rows.RemoveAt(dgv.CurrentRow.Index);
        }
于 2014-10-08T13:46:37.453 回答
-2
int s = 1;
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            for (int j = s - 1; j < dataGridView1.Rows.Count - 1; j++)
            {
                if (Convert.ToInt32(dataGridView1["Stok", j].Value) < 5)
                { s = j; dataGridView1.Rows.RemoveAt(j); break; }
            }
        }
        dataGridView1.Refresh();
于 2021-08-01T19:22:27.357 回答