C#在Datagridview中查找重复的值 [英] C# Find duplicate Values in Datagridview
问题描述
对我的c Sharp项目进行了一些用户界面限制. 使用Visual Studio 2008和C#.net.
doing some user interface restriction on my c sharp project. using visual studio 2008 and C#.net.
所以我有一些代码,它是一个嵌套的for循环,该循环应遍历各列行并检查是否存在重复.
So I have a little bit of code, its a nested for loop that should run through the columns rows and check if there's a duplicate.
考虑一下,我应该将文本更改为可以打印的数组,因为可以有多个重复项.
Thinking about it I should change the text to an array that I can print out since there can be more than 1 duplicate.
简而言之,有一个零件联盟,每增加一个零件就变得独一无二.用户希望更改联赛的组成部分,有些则上升,有些则下降.
Simply put, there is a league of parts, incrementing by one to be unique. The user wish's to change the league parts, some go up some go down.
这是我到目前为止所拥有的.
here's what I have so far.
public void CheckForDuplicate()
{
DataGridViewRowCollection coll = ParetoGrid.Rows;
DataGridViewRowCollection colls = ParetoGrid.Rows;
foreach (DataGridViewRow item in coll)
{
foreach (DataGridViewRow items in colls)
{
if (items.Cells[5].Value == item.Cells[5].Value)
{
if(items.Cells[2].Value != item.Cells[2].Value)
{
txtDupe.Text = items.Cells[2].Value.ToString();
this.Refresh();
dupi = false;
}
}
}
}
}
什么也没发生,似乎什么也没有,总是虚假的.奇怪的原因是调试没有捕获任何东西. 因此,如果我错过了一个愚蠢的班轮,或者是否有更好的方法来做这件事,我会在流浪吗?
Nothing happens, nothing at all seems it's always false. some odd reason debugging isn't catching anything. So I'm wandering if there's a silly one liner I've missed out, or if theres a much better way to do it?
非常感谢!
推荐答案
在LINQ中选择Distinct应该可以做到这一点.使用方法语法,类似:
Select Distinct in LINQ should do this. Using the method syntax, something like:
public bool allUniqueRows()
{
var distinctCount = from r in ParetoGrid.Rows
select r.whateverElement.Distinct().Count();
if(distinctCount == ParetoGrid.Rows.Count())
{
return true;
}
else
{
return false;
}
}
这篇关于C#在Datagridview中查找重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!