How to delete rows in the data grid view where the checkbox is checked?

I am using C # .NET 2.0 Visual Studio 2005.

I am facing a strange problem.

There is a simple window form with one DataGridView with a column1 flag (DataGridViewCheckboxColumn) .

Then, if the checkbox in the cell is checked, I want to delete the checked row.

The sound is very simple, but it does not delete all checked lines in any way, and I can’t understand why it behaves in this way.

For example, I have 5 lines and each checkbox is checked on each line, but it only removes 3 lines. Has anyone seen this before? Is this a mistake or am I doing something wrong?

namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //when I click the button, all checked row should be removed private void button1_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dataGridView1.Rows) { if ((bool)row.Cells[0].Value) { dataGridView1.Rows.Remove(row); } } } } } 
+6
source share
6 answers

this happens when one line is deleted in the same way as the number of lines, so if you put your code in a for loop and run it in the reverse order, it will work fine:

 for (int i = dataGridView1.Rows.Count -1; i >= 0 ; i--) { if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue) { dataGridView1.Rows.RemoveAt(i); } } 
+10
source

You change the collection when it repeats.

Use the delete list and delete the lines.

+6
source

You change the collection when it repeats. try it

 List<DataGridViewRow> toDelete = new List<DataGridViewRow>(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0].Value == true) { toDelete.Add(row); } } foreach (DataGridViewRow row in toDelete) { dataGridView1.Rows.Remove(row); } 
+4
source

@Chen Kinnrot, absolutely for the money! You will always delete only n% 2 lines at the start of your function, so if you have 10 lines, you should delete 5, and 101 - 51, etc. Iterate through the collection to determine which checkboxes are checked, and then delete these rows. A better solution would be to bind the event to a check box that automatically fires when button 1 is clicked.

+1
source

this solution gives a small error, I fixed adding 1 line :)

 List<DataGridViewRow> toDelete = new List<DataGridViewRow>(); foreach (DataGridViewRow row in dataGridView1.Rows) { bool s = Convert.ToBoolean(row.Cells[0].Value) //added this line if (s == true) { toDelete.Add(row); } } foreach (DataGridViewRow row in toDelete) { dataGridView1.Rows.Remove(row); } 
+1
source
 ASPXPAGE: <strong>Asp.Net : Delete Multiple Records form datagridview in one time<br /> </strong> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableModelValidation="True" ForeColor="Black"> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="id" HeaderText="Sr No" /> <asp:BoundField DataField="doc_name" HeaderText="Name" /> <asp:BoundField DataField="doc_add" HeaderText="Address" /> <asp:BoundField DataField="doc_mob" HeaderText="Mobile No" /> <asp:BoundField DataField="doc_email" HeaderText="Email" /> </Columns> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> </asp:GridView> <br /> <asp:Button ID="Button1" runat="server" Font-Size="12pt" onclick="Button1_Click1" Text="Delete" /> <br /> Code Behind Page: SqlConnection conn = new SqlConnection(@"server=server-pc; database=HMS; integrated security=true"); protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { load_data(); } } public void load_data() { SqlDataAdapter adp = new SqlDataAdapter("select * from doc_master", conn); DataSet ds = new DataSet(); adp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } protected void Button1_Click1(object sender, EventArgs e) { CheckBox ch; for (int i = 0; i < GridView1.Rows.Count; i++) { ch = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; if (ch.Checked == true) { int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text); SqlCommand cmd = new SqlCommand("delete from doc_master where ID=" + id + " ", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } load_data(); } 

For a detailed review of the code: http://www.gtuguide.com/2014/05/deleting-multiple-rows-in-gridview.html

0
source

Source: https://habr.com/ru/post/891109/


All Articles