Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

Please does anyone know how to loop through datagrid in C# Windows application and remove rows that the checkbox values are true, the same way yahoomail works. i have tried this:

C#
private void button1_Click(object sender, EventArgs e)
        {
            List<object > ChkedRow = new List<object >();
            DataRow dr;

            for ( int i = 0 ; i <= dataGridView1.RowCount - 1 ; i++ )
            {
                if ( Convert.ToBoolean( dataGridView1.Rows[i].Cells["chkcol"].Value ) == true )
                {
                    ChkedRow.Add( i );
                }
            }

            foreach ( int k in ChkedRow )
            {
                dr = dt.Rows[k];
                dt.Rows[k].Delete( );
                //dt.Rows.Remove(dr);
            }
        }


But the above code can only remove one record per time, i want to be able to remove more than one record if the user decides. Thanks
Posted

1 solution

Hi, it is because when you are deleting a row from a gridview the next row will take place of the deleted row.That means if you have stored the indexes of checkboxes which are true, after deleting a row now they have different indexes. I hope it is clear to you. So what you can do is keep a int type variable called countDeletedRows to keep how many rows you have deleted.Then use it as below to delete the records.

Note : i used dataGridView1.Rows.RemoveAt() to delete a datagrid row
Below code will delete all the rows which checkboxes are checked

C#
private void button1_Click(object sender, EventArgs e)
        {
            List<object> ChkedRow = new List<object>();
            int countDeletedRows = 0;

            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
                {
                    ChkedRow.Add(i);

                }
            }
            foreach (int k in ChkedRow)
            {
                dataGridView1.Rows.RemoveAt(k - countDeletedRows);
                countDeletedRows++;
            }
        }
 
Share this answer
 
Comments
Uwakpeter 8-Aug-14 9:32am    
Thanks, it works! i want to be able to delete from database also, i have tried this it doesn't works: dr = dt.Rows[k];
dt.Rows.RemoveAt( k - countDeletedRows );

The error message is:There is no row at position 1.
Dilan Shaminda 8-Aug-14 10:20am    
try it like this dr = dt.Rows[k - countDeletedRows]; I hope you want to delete the records from your DataTable dt not from database;
Uwakpeter 8-Aug-14 10:58am    
I will like to delete from database also, so that the record wont show next time the application is run. i dont know whether deleting from the datatable will aslo delete from database? Thanks
Dilan Shaminda 8-Aug-14 11:03am    
deleting datatable will not delete data from your database.refer some tutorials.here is a good one.
Step By Step Select, Insert, Update and Delete using ASP.NET C# and ADO.NET
Uwakpeter 8-Aug-14 11:07am    
Okay Thanks, i really appreciate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900