Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I want to delete item that has been checked in datagridview. but document also print double.

What I have tried:

C#
for(int xyz=0;xyz<datagridview1.rows.count;xyz++)>
            { 
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[xyz].Cells["Calckeck"];
                if (Convert.ToBoolean(chk.Value) == true)
                { 
                    DataTable dt = blord.getallfromorder_id(Convert.ToInt32(dataGridView1.Rows[xyz].Cells["cal_order_id"].Value));
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataGridViewRow dr in dataGridView1.Rows)
                        {
                            decimal total_to_be_remove = Convert.ToDecimal(dr.Cells["cal_total"].Value);
                            txtgrandtotal.Text = (Convert.ToDecimal(txtgrandtotal.Text) - Convert.ToDecimal(dataGridView1.Rows[xyz].Cells["cal_total"].Value)).ToString();
                            txtsubtotal.Text = (Convert.ToDecimal(txtgrandtotal.Text) - Convert.ToDecimal(dataGridView1.Rows[xyz].Cells["cal_total"].Value)).ToString();
                            lblsub_total.Text = txtsubtotal.Text;
                            printcalcel();
                            int i = blord.deleteitemfromorder(Convert.ToInt32(dataGridView1.Rows[xyz].Cells["cal_order_id"].Value));
                            dataGridView1.Rows.RemoveAt(xyz);
                        }                       
                    }
Posted
Updated 8-May-16 3:03am
v2
Comments
Karthik_Mahalingam 8-May-16 6:56am    
what is the issue ?
Member 12242717 8-May-16 7:38am    
it shows 2 times document print.
Sergey Alexandrovich Kryukov 8-May-16 9:58am    
What would you expect? You call printcalcel() inside loop and don't even show what is that.
Use the debugger, after all.
—SA

1 solution

Using .RemoveAt() inside a foreach loop is asking for trouble, there are other ways.
DataTable can filter the data, you could use DataTable.Select() to filter your data table, here is an example:
DataTable.Select Method (String) (System.Data)[^]
C#
var expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
    Console.WriteLine(foundRows[i][0]);
}
 
Share this answer
 
v3

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