Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:35:34+00:00 2026-06-06T05:35:34+00:00

I have a datable myTable, which is bind with a DataGridView dgv. The DataGridView

  • 0

I have a datable “myTable”, which is bind with a DataGridView “dgv”. The DataGridView “dgv” has a checkbox column. My goal is to delete rows checked in a button event. The datatable is updated of course.
Now my code is only working for deleting one row not for multiple rows.

Thanks for help.

 private void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (dgv.RowCount>0)
            {
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
                    if (check.Value != null)
                    {
                        if ((bool)check.Value)
                        {
                            DataRowView currentDataRowView = (DataRowView)dgv.CurrentRow.DataBoundItem;
                            DataRow dataRow = currentDataRowView.Row;

                            int n = dgv.CurrentRow.Index;
                            int intID = Convert.ToInt32(dgv.Rows[n].Cells[0].Value);
                            myTable.Rows.Remove(dataRow);
                            dgv.DataSource = myTable;

                            Int32 intVal = Convert.ToInt32(row.Cells[1].Value);
                            if (intVal == intID)
                            {
                                check.Value = null;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:35:36+00:00Added an answer on June 6, 2026 at 5:35 am

    I found the solution. The error was caused by

     DataRowView currentDataRowView = (DataRowView)dgv.CurrentRow.DataBoundItem; 
    

    currentDataRowView is not the checked row.
    The correct code is:

                    List<DataRow> toDelete = new List<DataRow>(); 
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        {
                            DataGridViewRow row = dgv.Rows[i];
                            DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
                            if (check.Value != null && (bool)check.Value)
                            {
                                DataRow dataRow = (row.DataBoundItem as DataRowView).Row;
                                toDelete.Add(dataRow);
                            }
                        }
                    }
                    toDelete.ForEach(row => row.Delete()); 
    

    Thanks everybody’s help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added a button column to a gridview. I am populating the gridview
I have a datable code to generate a string, which is sb.ToString(). DataTable dt
I have a KDB/Q databse which has around ~2M records per day consuming about
Have a matrix report now that has Position, Hours and Wages for a location
In a Dataset I have a datatable. This datable can be divided in a
I have to develop a application in .net technology which calls an external service
I have a get url input which I need to pass it into databse,
i have a table and in my table row there is one column for
I have a sql databse which is required to store a set of demographic
I have a Hosting which doesn't support SQL server or any other databse because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.