0

I am trying to delete multiple selected rows from a DataGridView. When I try the code below, it will only delete just a few of the selected rows. An example, I have seven rows, I select 5 consecutive rows and press delete and only three get deleted.

    IEnumerable<DataGridViewRow> dgvrs = from dgvrws in dgvChemicalInv.Rows.Cast<DataGridViewRow>()
                                                 where dgvrws.Selected.Equals(true)
                                                 select dgvrws;

            foreach ( DataGridViewRow dgr in dgvrs )
            {
                dctchemri = dgr.Cells["DCT_CHEMRI"].Value.ToString();
                index = dgvChemicalInv.CurrentRow.Index;

                var chemObj = ( from chmObj in DCTProjectNodeObj.Chemicals
                                where chmObj.DCTChemRI.Equals(dctchemri)
                                select chmObj ).Single();

                if ( sqlCmd.Delete_Chems(this.projID, (csDCTChemicalObj)chemObj) )
                {
                    dgvChemicalInv.Rows.Remove(dgr);
                }
                if ( dgvChemicalInv.RowCount > 0 )
                {
                    DCTProjectNodeObj.Chemicals.Remove((csDCTChemicalObj)chemObj);
                }
                else
                {
                    DCTProjectNodeObj.Chemicals = new List<csDCTChemicalObj>();
                    DO_BtnSaveClickEvent();
                }
            }

Thank you,

Bill O.

3 Answers 3

2

your code is quite hard to read. Anyway I'm sure the problem is in the foreach. In general you can't delete item within the foreach loop from the collection it is looping on. anyway I think the best choice would be remove the item from the datasource(maybe getting the id's from the repeater) and DataBind the repeater again

Sign up to request clarification or add additional context in comments.

2 Comments

I am doing a Linq query on the datagridview of all rows that are selected and trying to delete them in the foreach loop. In debug, step through I can watch the indexes change in the result view of the Enumerable dgvrs and these indexes correspond to the row index as the rows are being removed. As I walk through it now I can delete all but the last row.
here is the vary basics of the code code IEnumerable<DataGridViewRow> dgvrs = from dgvrws in dgvChemicalInv.Rows.Cast<DataGridViewRow>() where dgvrws.Selected.Equals(true) select dgvrws; foreach ( DataGridViewRow dgr in dgvrs ) { dgvChemicalInv.Rows.Remove(dgr); }
0

Your code is a little difficult to read, but I think that you are making this too complicated. I would recommend using something like a BindingSource object. It makes table management much easier. Set the datasource of the BindingSource to the table and the datasource of the DataGridView to the BindingSource object, and any changes made to the table will be reflected in the DataContext after calling SubmitChanges().

Example:

MyDataContext dataContext = new MyDataContext();
BindingSource bindingSource = new BindingSource();

bindingSource.DataSource = dataContext.MyTable;
dataContext.DataSource = bindingSource;

It's been a while since I've done this, but I think this is the easiest way to do it. This avoids any messy operations, however it can lead to complications with foreign key relationships.

Just a way that might make things simpler.

7 Comments

I have been looking over this possibility but it does not seem to work with a SqlCE database.
Didn't know you were working SqlCE. Not much experience with it, but what exactly doesn't work with SqlCE?
The connection and setting up the dataContext class. I was following along with this blog link
I was able to do what the blog said when using a regular sql database
Is this something that might help: pietschsoft.com/post/2009/01/…
|
0

I agree with @Massimiliano Peluso; in general, I would get all ids in one shot and make just one Database call passing a comma-separated list of ids to be deleted instead of calling one per row, unless of course, there are some other valid reasons for not doing it that way but calling the DB 200 times when you can call it once and be done seems unnecessary to me. I would then rebind the data and be done with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.