2012-07-04 137 views
4

我目前使用此代码:删除C#中的datagridview选定的行?

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

我加上勾下来的第一列,但与此代码,它只有被选中获得。我如何获得选中的复选框只有才能删除该行?

回答

5

你想要的东西像

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

我希望这有助于。

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); 
} 
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) 
       { 

       } 
      } 
     } 
    } 
0

试试这个:

if (dgv.SelectedRows.Count>0) 
     { 
      dgv.Rows.RemoveAt(dgv.CurrentRow.Index); 
     } 
相关问题