Removing selected rows in datagridview in c #?

I am currently using this code:

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

I have checkboxes down the first column, but with this code it only gets the selected one. How to get selected CheckBoxes for deletion only using string?

+4
source share
4 answers

Do you want something like

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

Hope this helps.

+5
source

Try something like this:

 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
source

It might be something like this ... This is an example for listview, however the concept almost exists. Scroll through the item and find the checkbox identifier and delete the selected ones. Hope this helps.

 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) { } } } } 
+1
source

Try the following:

 if (dgv.SelectedRows.Count>0) { dgv.Rows.RemoveAt(dgv.CurrentRow.Index); } 
0
source

All Articles