如何检查DataGridview中的列中是否有任何重复值? [英] How to check if there are any duplicate values in a Column in a DataGridview?

查看:112
本文介绍了如何检查DataGridview中的列中是否有任何重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,在保存数据之前,我想检查某个特定列是否在任何行中都有重复值。

I have a DataGridView and before saving data I would like to check if a particular column has any duplicate values in any rows.

If DataGridView1.Rows.Count > 2 Then
            Dim count As Integer = 0
            Dim i As Integer = 0
            While DataGridView1.Rows.Count - 1
                Dim j As Integer = 1
                While DataGridView1.Rows.Count - 1
                    Dim str As String = DataGridView1.Rows(i).Cells("ColLedger").Value()
                    Dim str1 As String = DataGridView1.Rows(j).Cells("ColLedger").Value()
                    If str1 = str Then
                        count = count + 1
                    End If
                    j += 1
                End While
                i += 1
            End While
            If count > 0 Then
                MsgBox(count)
            End If



我的索引超出范围错误。我不确定我做错了什么。

如果可以使用在线工具轻松转换,我也会接受c#的答案。


I am getting index out of range error. I am not sure what I am doing wrong.
I will accept c# answers too if they can be easily converted using online tools.

推荐答案

首先,您的错误是由您的While循环条件引起的。在你的代码中,while循环将永远不会退出,导致索引异常,因为循环不断迭代超出网格中的最后一行。



我个人在这种情况下不会使用while循环,因为您事先知道要根据行数循环多少次。如果您使用for ...每个循环,则更容易阅读代码。



也就是说,为了修复您的代码,您需要澄清您的虽然是条件所以循环可以退出。



在你的代码中你有以下while循环...

First, you error is being caused by your While loop conditions. In your code you While loop will never exit, resulting in an out-of-index exception as the loop keeps iterating beyond the last rows in your grids.

I personally wouldn't use while loops in this scenario because you know up front how many times you want to loop based on the number of rows. It's easier to read the code if you use a for...each loop instead.

That said, to fix your code as you have it you need to clarify your While's condition so the loop can exit.

In your code you have the following While loop...
While DataGridView1.Rows.Count - 1
    '... some code
End While



你的While条件基本上是'While True',因为你没有将行数与任何东西进行比较。您想检查行数是否小于循环计数(您的j和i变量)。为此,请将循环更改为此类...


Your While condition is basically saying 'While True' because you are not comparing the row count against anything. You want to check the row count is less than the cycle count (your j and i variables). To do this change your loops to something like this...

While i <= DataGridView1.Rows.Count - 1
    '... some code
End While



这是您的代码,但已更正,因此While循环将退出...


Here is your code but corrected so the While loops will exit...

If DataGridView1.Rows.Count > 2 Then
    Dim count As Integer = 0
    Dim i As Integer = 0

    ' loop condition will loop while the row count is less or equal to i
    While i <= DataGridView1.Rows.Count - 1
        Dim j As Integer = 1

        ' loop condition will loop while the row count is less or equal to j
        While j <= DataGridView1.Rows.Count - 1
            Dim str As String = DataGridView1.Rows(i).Cells("ColLedger").Value()
            Dim str1 As String = DataGridView1.Rows(j).Cells("ColLedger").Value()
            If str1 = str Then
                count = count + 1
            End If
            j += 1

        End While

        i += 1
    End While

    If count > 0 Then
        MsgBox(count)
    End If

End If


Hello Jim,



请使用此项代码,我还没有验证语法,但这个逻辑应该有用。



Hello Jim,

Please use this code, i have not verified syntax but this logic should work.

if(DataGridView1.Rows.Count > 2){
                int count = 0;
                for(int r=0; r<=DataGridView1.Rows.Count - 1; r++)
                {
                   for(int s=0; s<=DataGridView1.Rows.Count - 1; s++)
                   {
                      string str = DataGridView1.Rows[r]["ColLedger"].Value();
                      string str1 = DataGridView1.Rows[s]["ColLedger"].Value();
                      if(str == str1)
                        count = count + 1;
                   }
                }
                if(count > 0)
                {
                     MsgBox(count);
                }
            }





谢谢,

Imdadhusen



Thanks,
Imdadhusen


这篇关于如何检查DataGridview中的列中是否有任何重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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