0

Can somebody tell me my mistake? I have two Tables, merged in one. They are distinguishable by one column called source. (expected, found) I wan´t to remove all equal rows... But in case of removing both rows, only the row i-1 will be removed...

for (int i = 0; i < tbl.Rows.Count; i++)
            {
                try
                {

                    if (tbl.Rows[i].ItemArray.Contains(Constants.VALUE_SOURCE_FOUND) && tbl.Rows[i - 1].ItemArray.Contains(Constants.VALUE_SOURCE_EXPECTED))
                    {

                        var founded = (tbl.Rows[i].ItemArray).ToArray();
                        var expected = (tbl.Rows[i - 1].ItemArray).ToArray();


                        for (int u = 0; u < founded.Length; u++)
                        {
                            if (founded[u].ToString() == Constants.VALUE_SOURCE_FOUND)
                            {
                                founded = founded.Where(w => w != founded[u]).ToArray();        
                                expected = expected.Where(w => w != expected[u]).ToArray();     
                            }
                        }


                        if (founded.ToString() == expected.ToString())
                        {
                            tbl.Rows[i - 1].Delete();       
                            tbl.AcceptChanges();
                            tbl.Rows[i].Delete();           
                            tbl.AcceptChanges();
                        }

                    }
                }
                catch { }
            }
        }


        return tbl;
Nick S.
  • 11
  • 3

1 Answers1

0

Does this work?

tbl.Rows[i].Delete();
tbl.AcceptChanges();

tbl.Rows[i-1].Delete();

tbl.AcceptChanges();

i = i - 2;

As far as i know, delete only "marks" the row as to be deleted, you have to commit it first. With your second call you commit the first one, but the second delete is never executed on the datatable.

Nick S.
  • 11
  • 3