如何在dataGridView中比较2行并突出显示不同的单元格? [英] how to compare 2 rows in a dataGridView and highlight the different cells?

查看:91
本文介绍了如何在dataGridView中比较2行并突出显示不同的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较dataGridView中的两个选定行?
1.-我需要知道如何检测两个选定的行
2.-比较两个选定的行
3.-突出显示差异cells

how to compare two selected rows in a dataGridView? 1.- I need to know how to detect the two selected rows 2.- Compare the two selected rows 3.- highlight the differences "cells"

我尝试了这个但不幸的是我失去了.....

I've tried this but unfortunately I am lost.....

DataTable src1 = dataGridView1.DataSource as DataTable; //THIS IS PROBABLY NOT NEEDED
    DataTable src2 = dataGridView1.DataSource as DataTable;
        int index1 = 0;

        for (int i = 0; i < src1.Rows.Count; i++)
        {
            var row1 = src1.Rows[i].ItemArray;
            var row2 = src2.Rows[i].ItemArray;

            for (int j = 0; j < row1.Length; j++)
            {
                if (!row1[j].ToString().Equals(row2[j].ToString()))
                {
                    dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
                    dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
                }
            }
        }


推荐答案

其实你的代码不好,我会回答你的问题:

Actually your code is not good i will answer your questions :

1.我需要知道如何检测两个选定的行

1.- I need to know how to detect the two selected rows

dataGridView1.SelectedRows

2.-比较两个选定的行,应该看起来类似于以下内容:

2.- Compare the two selected rows it should look to something similar to follow :

for (int i = 0; i < dataGridView1.SelectedRows.Count-1; i++)
{
    for (int j = 0; j < dataGridView1.SelectedRows.rows[i].Cells.Count; j++)
    {
        if(dataGridView1.SelectedRows.rows[i].Cells[j].value.Equals(dataGridView1.SelectedRows.rows[i+1].Cells[j].value))
        {
            dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
            dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;
        }
    }    
}

3.-突出差异cell

3.- highlight the differences "cells"

  dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
  dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;

这篇关于如何在dataGridView中比较2行并突出显示不同的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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