Jump to content

Remover dados repetidos no datagridview


sabing
 Share

Recommended Posts

Boa tarde, eu estou com dificuldade de fazer uma lógica de remover dados repetidos em um datagridview , exemplo na tabela tem 4 valores  e tenho 1 valor repetidos eu preciso indetificar um valore repetido e remover esse valor eu fiz um lógica , mais não funciona corretamente pode me ajudar

 

Sub Limpar_dados_repetidos()
        If TabelaFazenda.Rows.Count() > 1 Then
            For j As Integer = TabelaFazenda.Rows.Count() - 1 To 0 Step -1
                Dim s As Boolean = False
                For x As Integer = 0 To TabelaFazenda.Rows.Count() - 1
                    If TabelaFazenda.Rows(x).Cells(0).Value = TabelaFazenda.Rows(j).Cells(0).Value Then
                        If s = False Then
                            TabelaFazenda.Rows(x).DefaultCellStyle.BackColor = Color.Red
                            s = True
                        End If
                    End If
                Next
            Next
            For j As Integer = TabelaFazenda.Rows.Count() - 1 To 0 Step -1
                If TabelaFazenda.Rows(j).DefaultCellStyle.BackColor = Color.Red Then
                    Me.TabelaFazenda.Rows.RemoveAt(j)
                End If
            Next
        End If
    End Sub

 

Link to comment
Share on other sites

Eu fiz esse outro metodo , parece que funcionou

If TabelaFazenda.Rows.Count() > 1 Then
            Dim s As Boolean = False
            For i As Integer = 0 To TabelaFazenda.Rows.Count() - 1
                For j As Integer = 0 To TabelaFazenda.Rows.Count() - 1
                    If Not (i = j) Then
                        If s = False Then
                            If TabelaFazenda.Rows(j).Cells(0).Value = TabelaFazenda.Rows(i).Cells(0).Value Then
                                TabelaFazenda.Rows(i).DefaultCellStyle.BackColor = Color.Red
                                s = True
                            Else
                                s = False
                            End If
                        End If
                    End If
                Next
            Next
            For j As Integer = TabelaFazenda.Rows.Count() - 1 To 0 Step -1
                If TabelaFazenda.Rows(j).DefaultCellStyle.BackColor = Color.Red Then
                    Me.TabelaFazenda.Rows.RemoveAt(j)
                End If
            Next
        End If

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.