Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello


how are you friends :)

regrading to the subject of the topic i want to know how to compare between 2 datagridview and color the different

i have a time sheet for employee i want to compare the employee IDs in the time sheet (excel sheet loaded into a datagridview1 ) with datagridview2 that include all employee code (Loaded from the database ). i want if there are any different between datagridview1 and datagridview2 to be marked with red color

here is my code

could you please help me to make it work

C#
private void btncompare_Click(object sender, EventArgs e)
       {
           DataTable src1 = dataGridView1.DataSource as DataTable;
           DataTable src2 = dataGridView2.DataSource as DataTable;
           int index1 = 0;

           foreach (DataRow row1 in src1.Rows)
           {
               foreach (DataRow row2 in src2.Rows)
               {
                   int index2 = 0;
                   bool duplicateRow = true;
                   for (int cellIndex = 0; cellIndex < row1.ItemArray.Count(); cellIndex++)
                   {
                       if (!row1.ItemArray[cellIndex].Equals(row2.ItemArray[cellIndex].ToString()))
                       {
                           duplicateRow = true;
                           dataGridView1.Rows[index1].DefaultCellStyle.ForeColor = Color.Yellow;
                           break;
                       }
                   }

                   if (duplicateRow)
                   {
                       dataGridView1.Rows[index1].DefaultCellStyle.ForeColor = Color.Red;
                   }

                   index2++;
               }
               index1++;
           }
       }


Thanks in advance
Posted
Comments
Suvendu Shekhar Giri 11-Oct-15 9:54am    
The code looks OK although there are many otherways you can do it. What happens in run time? Doesn't it work ?
OriginalGriff 11-Oct-15 9:55am    
And?
What does it do that you didn't expect, or not do that you did?
Developer It 11-Oct-15 10:20am    
its working but mark all the values in dataGridView1 is red that mean the different id also became in red color

The chance are it's the comparison that is failing: the DataRow.ItemArray[index] value will be an object - which means that whatever you compare it with will be a reference comparison. And a reference comparison doesn't care about the content - it only cares that the two items are the same object (they reference the same object on the heap). If you like, it's like having two people called "John Smith" - the names are the same (they have the same value) but one has a moustache and the other doesn't which means they are different people (they have different references).

The code you are using compares to see if the two men are the same person, you want a check that compares them for having the same name!

Try this:
C#
if (row1.ItemArray[cellIndex].ToString() != row2.ItemArray[cellIndex].ToString())
And see if that works any better.
 
Share this answer
 
v2
this is the solution

thanks guys for helping me

C#
private void checkGridData()
      {
          int RowCount1 = 0, RowCount2 = 0;
          RowCount1 = dataGridView1.Rows.Count;
          RowCount2 = dataGridView2.Rows.Count;
          bool ItemFound = false;
          string Value1  , Value2 ;
          int diif_num = 0;
          for (int LCount1=0 ; LCount1<rowcount1-1;lcount1++)>
          {

              ItemFound = false;
              for (int Lcount2 = 0; Lcount2 < RowCount2-1; Lcount2++)
              {
                  //Value1 = double.Parse(dataGridView1.Rows[LCount1].Cells[0].Value.ToString());
                  //Value2 = double.Parse(dataGridView2.Rows[Lcount2].Cells[0].Value.ToString());

                  Value1 = dataGridView1.Rows[LCount1].Cells[0].Value.ToString();
                  Value2 = dataGridView2.Rows[Lcount2].Cells[0].Value.ToString();
                  if  (Value1==Value2)
                  {
                      ItemFound = true;
                      break;
                  }
              }
              if (ItemFound == false)
              {
                  diif_num = diif_num +1;
                  dataGridView1.Rows[LCount1].DefaultCellStyle.BackColor = Color.Red;
              }
          }

          label1.Text = @"Affected IDs is : " + diif_num.ToString();
      }
 
Share this answer
 

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