Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare rows of the datatable 1

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
I have two DataTables
I have matched up the corresponding rows based on a primary key

The next step is that I need to compare each of the two matched rows to see if there is any difference in any of the fields

Is the only way to do this is to compare every single field?

Thank you
 
the answer is yes... but this is a simple loop problem.
make a procedure that returns a bool type and gets as params the two rows. then inside use:
Code:
if (myRow1.ItemArray.Length != myRow2.ItemArray.Length) return false; // the rows don;t have the same number of fields
for (int i=0; i < myRow1.ItemArray.Length; i++)
     if (myRow1[i] != myRow2[i]) return false; // if one field differs from the other return false
return true; // else if all is ok, return true
 
Thanks for the advice.
I wonder if you could help me with another issue.

Once I found the rows that are different, I am importing them into a new datatable, which has an extra column, to say (update/new/OR delete)

The code looks like this
//import row into new table
dt_result.ImportRow(dt_old.Rows[j]);

Now all I have to do is to add the string "update" to the same row under column 'status'

But I don't know the row number?

The only way I can think is to set a primary key, and look for the row with that primary key, but that is like looping through again.

Any easier solution?

Marika

 
do it like this:
Code:
DataRow dr = dt_result.NewRow();
for (int i=0; i < dt_old.Rows[j].ItemArray.Length; i++)
    dr[i] = dt_old.Rows[j][i];
dr[dt_old.Rows[j].ItemArray.Length] = "new";
dt_result.Rows.Add(dr);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top