Solved

Deleting rows from a DataGridView control.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert datetime to time string 10 28
MailAddress in vb 4 29
call a controller action method from Global.asax in asp.net 1 20
Name Space error VS2015 1 23
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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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