Datagridview,查找重复行并更新现有数据 [英] Datagridview, find duplicate rows and update existing data

查看:758
本文介绍了Datagridview,查找重复行并更新现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Datagridview,仅显示唯一值为重复单元格值C#2005

我想知道如何在datagridview中查找重复的行,如果存在只能更新数据..我需要代码来做,我在C#和.NET Framework 2.0 VS2005。我没有使用数据库是来自Active Directory的查询,值继续重复。

I want to know how to find duplicate rows in a datagridview and if exist only update the data.. i need the code to do that I'm working in C# and .NET framework 2.0 VS2005. I'm not using a database is from query from Active Directory and the values continue repeating.

有人可以帮助这个...谢谢

can someone helpme with this...Thanks

推荐答案

你'需要循环遍历每一行,然后通过每一列。最好的方法是在将它们放在 DataGridView 中之前查找重复,因为没有内置的方法可以使用 DataGridView

You'd need to loop through each row and then through each column. The best way would be to find duplicate before putting them in the DataGridView because there is no built-in way to do it with a DataGridView.

bool isDuplicate;

for(int nbRow = 0; nbRow < DataGridView1.Rows.Count; nbRow++)
{
    for(int nbRowCompare = nbRow; nbRowCompare < DataGridView1.Rows.Count; nbRowCompare++)
    {
        isDuplicate = true;

        for(int nbCol = 0; nbCol < DataGridView1.Rows[nbRow].Cells.Count; nbCol++)
        {
            if(DataGridView1[nbCol, nbRow].Value != DataGridView1­[nbCol, nbRowCompare])
            {
                isDuplicate = false;
                break;     //Exit for each column if they are not duplicate
            }
        }

        if(isDuplicate)
        {
             //Do something
        }
    }
}

注1:您的数据可能有一个模式,您可以减少比较数量。这个例子比较每一行与以下每一行。


注2:如果你想删除重复的内容,你应该遍历从最后到首先

Note 1: Your data might have a pattern where you can reduce the number of comparison. This example compare every row against every following rows.

Note 2: If you want to delete duplicates, you should iterate through the rows from the last to the first

这篇关于Datagridview,查找重复行并更新现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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