如何比较2 datagridview和color之间的不同 [英] how to compare between 2 datagridview and color the different

查看:92
本文介绍了如何比较2 datagridview和color之间的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好





你好朋友:)



重新审视主题的主题我想知道如何比较2 data.gridview和颜色之间的不同



i有一个员工的时间表我想比较员工ID在包含所有员工代码(从数据库加载)的datagridview2的时间表(excel表加载到datagridview1中)。我想如果datagridview1和datagridview2之间有任何不同用红色标记



这里是我的代码



你可以帮助我让它工作吗



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

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

推荐答案

失败的比较是偶然的:DataRow.ItemArray [index]值将是一个对象 - 这意味着无论你将它与它进行比较,都将是一个参考比较。参考比较并不关心内容 - 它只关心这两个项是同一个对象(它们引用堆上的同一个对象)。如果你喜欢,就像有两个人叫约翰史密斯 - 名字是相同的(他们有相同的价值),但一个人留着小胡子,另一个并不意味着他们是不同的人(他们有不同的参考) 。



你正在使用的代码比较两个人是否是同一个人,你想要一个比较他们的同名的支票!



试试这个:

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:
if (row1.ItemArray[cellIndex].ToString() != row2.ItemArray[cellIndex].ToString())

看看它是否更好。


这是解决方案



感谢各位帮帮我



this is the solution

thanks guys for helping me

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();
      }


这篇关于如何比较2 datagridview和color之间的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆