Solved

Deleting rows from a DataGridView control.

Posted on 2008-10-14
7
1,923 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 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 125 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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