Solved

Deleting rows from a DataGridView control.

Posted on 2008-10-14
7
1,921 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
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 42
C#.net split string and then check appropriate checkboxlist 4 44
Easy filter aspnet 2 35
Input parameteres to DragOver 2 38
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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