Solved

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

Posted on 2009-05-20
2
566 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
[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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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