Click here to Skip to main content
15,936,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this question or a similar has been asked many times over...
I already did this code to compare two datatables. what i need is something a bit more complex.

What I have is a DataTable names originalTable, populated with some data from a DB.
Lets say the data is as follows:

Col-1 Col-2 Col-3
1 a x
2 b y
3 c s
4 d q
5 e w

Now, I make an exact copy of this table using

C#
DataTable copyTable = originalTable.Copy();


The copyTable is used for cross checking purposes and will not be modified. The original table will be bound to a DataGridView and may get modified.

The modification can be 3 types:

Insertion of a whole new row, Modification of existing row or Deletion of existing row.

for example, suppose row 3 gets deleted and I add a new row, namely Row 6 in the same index/position of row 3 in the DataGridView.

Col-1 Col-2 Col-3
1 a x
2 b y
6 f r
4 d q
5 e w

What i need to do is that, I need to do something like click 'save' and the below happens:


C#
originalTable.AcceptChanges();
DataTable updatedData = CompareTables(originalTable, copyTable);



Here, i need the updatedData to correctly identify that

Row3 has been deleted, Row6 has been added in place of row3, thus, this is not an update operation, rather a delete and an insert operation...

Or, say I add row7 to the end.
before i save, i can see the row7 in the datagridview. i modify and modify and modify row7 again and again. In this case, CompareTables must recognizethat it is an INSERT operation as row7 has not been inserted. So, row7 has to be inserted with the latest modification that was done to it before 'save' was clicked.

It is necessary that the number 1, 2, 3, 4... which i used for row reference be unique and never duplicated, even though the row has been marked as deleted.
new row must always have the MAX(number) + 1, ie, wherever i insert the new row doesnt change the fact that its number will be greater than the latest row number and progress in a consecutive fashion.

I already have this function:

C#
public static DataTable CompareRows(DataTable sourceTable, DataTable checkTable)
		{
			DataTable resultTable = sourceTable.Clone();
			resultTable.Clear();
			if (checkTable.Rows.Count == 0)
			{
				resultTable = sourceTable.Copy();
			}
			else
			{
				for (int i = 0; i < checkTable.Rows.Count; i++)
				{
					var sourceArray = sourceTable.Rows[i].ItemArray;
					var checkArray = checkTable.Rows[i].ItemArray;
					if (!sourceArray.SequenceEqual(checkArray))
					{
						DataRow rtRow = resultTable.NewRow();
						rtRow.ItemArray = sourceTable.Rows[i].ItemArray;
						resultTable.Rows.Add(rtRow);
					}
				}
			}
			return resultTable;
		}


But this does not do the job in every situation. I need a very general compare function that can correctly identify every INSERT, MODIFY and DELETE performed on the originalTable and return those rows with the appropriate markers, maybe in a DataSet having 3 tables, namely
insertTable, modifyTable and deleteTable, each containing the appropriate rows...
Posted
Comments
Sushil Mate 5-Jul-13 4:54am    
can you shortened your query?

1 solution

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