Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGridView and before saving data I would like to check if a particular column has any duplicate values in any rows.
VB
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

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.
Posted
Updated 27-Dec-13 0:49am
v2

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...
VB
While DataGridView1.Rows.Count - 1
    '... some code
End While

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...
VB
While i <= DataGridView1.Rows.Count - 1
    '... some code
End While

Here is your code but corrected so the While loops will exit...
VB
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
 
Share this answer
 
Hello Jim,

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

C#
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);
                }
            }


Thanks,
Imdadhusen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900