C# datagridview comparison....
how to compare datagridview's and display the difference in another grid?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Aug 20, 2012, 3:20 AM EDT
public bool compareTbls(Datatable tbl1, Datatable tbl2)
{
if(tbl1.Rows.Count != tbl2.Rows.Count || tbl1.Columns.Count != tbl2.Columns.Count)
return false;
for(int i = 0; i < tbl1.Rows.Count; i++)
{
for(int c = 0; c < tbl.Columns.Count; c++)
{
if(tbl1.Rows[i][c] != tbl2.Rows.Columns[i][c])
return false;
}
}
return true;
}
Also if you want to compare using linq,
then check below linq.
http://www.codeproject.com/Tips/344792/Compare-two-datatable-using-LINQ-Query
hj jh81 reputationPosted Aug 20, 2012, 2:03 AM EDT
Gopal Mahadak136 reputationPosted Aug 20, 2012, 2:09 AM EDT
name password
abc 123
xyz 321
asd 147
jkl 963
another datagridview contains:
name password
abc 123
xyz 321
So compare both... & display its difference in another datagridview.. please help..
trying from one day... send code...
Sukesh MarlaRank #461 · 5.4K reputationPosted Aug 20, 2012, 2:15 AM EDT
Comparing datatable will be a good idea instead of grid
Check this as correct answer if helped
Gopal Mahadak136 reputationPosted Aug 20, 2012, 2:23 AM EDT
send code
hj jh81 reputationPosted Aug 20, 2012, 3:23 AM EDT
private void SampleSolution(DataTable dt1, DataTable dt2) { //If you have primary keys: var results = from table1 in dt1.AsEnumerable() join table2 in dt2.AsEnumerable() on table1.Field("id") equals table2.Field("id") where table1.Field("ColumnA") != table2.Field("ColumnA") || table1.Field("ColumnB") != table2.Field("ColumnB") || table1.FieldString>("ColumnC") != table2.FieldString>("ColumnC") select table1; //This will give you the rows in dt1 which do not match the rows in dt2. You will need to expand the where clause to include all your columns. //If you do not have primarry keys then you will need to match up each column and then find the missing. var matched = from table1 in dt1.AsEnumerable() join table2 in dt2.AsEnumerable() on table1.Field("ColumnA") equals table2.Field("ColumnA") where table1.Field("ColumnB") == table2.Field("ColumnB") || table1.Field("ColumnC") == table2.Field("ColumnC") || table1.Field("ColumnD") == table2.Field("ColumnD") select table1; var missing = from table1 in dt1.AsEnumerable() where !matched.Contains(table1) select table1; //This should give you the rows which do not have a match. You will need to expand the where clause to include all your columns. } or you can also iterate through each of the rows of datatable1 & compare its value to each of the row in databale2