How can I correctly delete multiple rows in a datagridview control?

Posted on 2007-08-09
Last Modified: 2013-11-05
Hi, I want to delete the selected rows in a datagridview that has a datatable as a datasource.  I loop through the selected rows one by one but then I get a excption error that indexing is out of bound.  The indexing changes when I deleting one by one so that doesn't work.  Any good way to get this done?  Thanks.
        private void btnMulExclude_Click(object sender, EventArgs e)
            DataGridView dgvForMapping, dgvExclude;

            if (filterUser)
                dgvForMapping = dgvSource;
                dgvExclude = dgvFiltered;
                dgvForMapping = dgvGroupSource;
                dgvExclude = dgvGroupFiltered;


            if (dgvForMapping.SelectedRows.Count <= 0)
                MessageBox.Show("Please select at least 1 row to filter.");
            DataRow drTarget, drSource;
            DataRowView drv;
            int rowsToExclude = dgvForMapping.SelectedRows.Count;

            for (int index = 0; index < rowsToExclude; index++)
                drTarget = tblExcludeRows.NewRow();
                drv = (DataRowView)(dgvForMapping.SelectedRows[index].DataBoundItem);
                drSource = drv.Row;
                CUnityDS.InsertDataRow(ref drTarget, drSource);
            dgvExclude.DataSource = tblExcludeRows;

Question by:lapucca
    LVL 13

    Accepted Solution

    Hi lapucca

    Try using the following code to remove selected rows from a dataGridView Control:

                foreach (DataGridViewRow dr in dataGridView1.SelectedRows)

    Author Comment

    That worked perfectly.  Thanks.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    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.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    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…

    734 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

    25 Experts available now in Live!

    Get 1:1 Help Now