?
Solved

Deleting rows from a DataGridView control.

Posted on 2008-10-14
7
Medium Priority
?
1,927 Views
Last Modified: 2013-12-17
Hi:

I'm trying to figure out the best way to delete rows from my dataGridView control.
The dataGridView contains 2 colums: 1 - CheckBox; 2 - String

I would like to be able to delete the rows based on whether the checkbox is selected or not.

My code looks like this:

                // Loop through and remove the rows where the value is checked
                foreach (DataGridViewRow dgvExistingRow in this.dataGridViewFileNames.Rows)
                {
                    if (dgvExistingRow.Cells["FileCheckBox"].Value.Equals(true))
                    {
                        this.dataGridViewFileNames.Rows.Remove(dgvExistingRow);
                    }
                }

This isn't quite working right. If all the rows are to be removed (they are all checked), then only N-1 rows are removed,
where N is the number of rows in the row collection.

I know this should be relatively straightforward, but I'm just not seeing it.

Thanks in advance,
JohnB
0
Comment
Question by:jxbma
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22718890
You have to do a cycle from the last row to the first in order to delete.
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22719426
Have a look at the following link
it explains the functionality as you require
http://www.dotnetjohn.com/articles.aspx?articleid=51
0
 
LVL 8

Expert Comment

by:OBonio
ID: 22719440
You can't modify a collection whilst iterating through it.  ie,  you are not allowed to remove row from datagrid while doing a foreach on the rows collection.

You need to remove them outside of the foreach loop.  To do this, create an ArrayList which you add the row to in the foreach loop then iterate the ArrayList after the foreach, removing each row.

That should do the job.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22719455
If you need help with the code just say it!
I can show you a code snippet.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22901984
Hi, my comment ID:22719426 indicates a complete coding example as to how to implement his/her required functionality
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question