Solved

Select Rows in the DataGridView and delete them in the Datatable?

Posted on 2009-05-20
2
561 Views
Last Modified: 2012-05-07
Hello,

how can delete those rows in the DataTable, which I have selected in the DataGridView ?
private void moveArticlesToFilterButton_Click(object sender, EventArgs e)

        {

            foreach (DataGridViewRow row in articlesDataGridView.SelectedRows)

            { 

                articlesChoiceDataTable.Rows.Add(row.Cells[2].Value.ToString(),

                                                 row.Cells[3].Value.ToString(),

                                                 row.Cells[4].Value.ToString(),

                                                 row.Cells[5].Value.ToString(),

                                                 row.Cells[6].Value.ToString(),

                                                 row.Cells[7].Value.ToString());
 

                articlesDataTable.Rows.Remove(row); // does not work

            }

             

        }

Open in new window

0
Comment
Question by:MSFanboy
2 Comments
 
LVL 3

Accepted Solution

by:
SafeCode earned 500 total points
ID: 24429576
Hi,

The item can't be removed from collecting while iterating within it.

Two ways to resolve this issue are:
1. Create temporary collection of items to be removed.  Fill temporary collection with items to be removed from original collection.  Iterate through temporary collection to remove items from original collection.
2. Use for loop.
private void moveArticlesToFilterButton_Click(object sender, EventArgs e)

        {

            for (int i = articlesDataGridView.SelectedRows.Count - 1; i <= 0; i--)

            {             articlesChoiceDataTable.Rows.Add(articlesDataGridView.SelectedRows[i].Cells[2].Value.ToString(),

     articlesDataGridView.SelectedRows[i].Cells[3].Value.ToString(),

     articlesDataGridView.SelectedRows[i].Cells[4].Value.ToString(),

     articlesDataGridView.SelectedRows[i].Cells[5].Value.ToString(),

     articlesDataGridView.SelectedRows[i].Cells[6].Value.ToString(),

     articlesDataGridView.SelectedRows[i].Cells[7].Value.ToString());

     articlesDataGridView.SelectedRows.RemoveAt(i); 

            }

             

        }

Open in new window

0
 

Author Closing Comment

by:MSFanboy
ID: 31583388
I did not read your solution but I did the same, deleted the rows in the DGV instead of DT ;P
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now