Removing all duplicated rows in datagridview by one click

topsykretts

Well-known member
Joined
Dec 22, 2011
Messages
47
Programming Experience
Beginner
Hello,
I tried with code above but it removes only one duplicate by click.
As source I am using text file that has a words separated by ",". Content of text file: (Jacob,Jonah,Adam,Shawn,Aaron,Liam,Daniel,Ryan,Adam,Luke,Jacob)

01.png

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Name As New DataGridViewTextBoxColumn
        Name.Name = "Name"
        Name.HeaderText = "Name"
        Name.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        Name.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Name.Width = 200
        Name.SortMode = 0
        DataGridView1.Columns.Add(Name)

    End Sub

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

        'Open TXT
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        openFileDialog1.Title = "Text Documents"
        openFileDialog1.Filter = "Text Documents (*.txt)|*.txt"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            'Clear DGW
            DataGridView1.Rows.Clear()

            Dim stFilePathAndName As String = openFileDialog1.FileName
            TextBox1.Text = stFilePathAndName
            Dim sr As IO.StreamReader = New IO.StreamReader(stFilePathAndName)
            Dim line As String
            Do While Not sr.EndOfStream
                line = sr.ReadLine()
                Dim aryTextFile() As String
                aryTextFile = line.Split(",")
                For i = 0 To UBound(aryTextFile)
                    DataGridView1.Rows.Add(aryTextFile(i))
                Next i
                ' Sort first column, ascending:
                DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
                If line = Nothing Then Continue Do ' This line skips blanks
            Loop
            sr.Close()
        Else
            openFileDialog1.Dispose()
        End If
    End Sub
    
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    
    Private Sub btnRemoveDuplicates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveDuplicates.Click
     
            For intI As Integer = 0 To DataGridView1.Rows.Count - 1
                For intJ As Integer = intI + 1 To DataGridView1.Rows.Count - 1
                    If DataGridView1.Rows(intI).Cells(0).Value = DataGridView1.Rows(intJ).Cells(0).Value Then
                        DataGridView1.Rows.RemoveAt(intJ)
                        Exit Sub
                    End If
                Next intJ
            Next intI

    End Sub
 
If you want to loop through a collection and conditionally remove items then you should loop from the end to the beginning rather than the beginning to the end. That way, removing an item will not affect the indexes of any remaining items.

By the way, the reason that your code only removes one item is that you Exit Sub as soon as you remove the first item. If you simply delete that line though, your code will crash the app. The outer loop needs to go backwards. The inner loop should also go backwards but it does really matter, as long as you use the correct limits. When you remove an item, use the loop counter from the outer loop as the index, NOT the inner loop counter.
 
If you want to loop through a collection and conditionally remove items then you should loop from the end to the beginning rather than the beginning to the end. That way, removing an item will not affect the indexes of any remaining items.

By the way, the reason that your code only removes one item is that you Exit Sub as soon as you remove the first item. If you simply delete that line though, your code will crash the app. The outer loop needs to go backwards. The inner loop should also go backwards but it does really matter, as long as you use the correct limits. When you remove an item, use the loop counter from the outer loop as the index, NOT the inner loop counter.

Hello jmcilhinney,
I am not a programer and this is too difficult for me but I am trying to learning. I like .net and have some small application which I made using .net, but I expect that someone help me and says: "There is a problem, remove this line, put this code there."

I made modification but it doesn't works.
VB.NET:
 For intI As Integer = 0 To DataGridView1.Rows.Count - 1
            For intJ As Integer = intI + 1 To DataGridView1.Rows.Count - 1
                If DataGridView1.Rows(intI).Cells(0).Value = DataGridView1.Rows(intJ).Cells(0).Value Then
                    DataGridView1.Rows.RemoveAt(intJ)
                End If
            Next
        Next intI
 
I expect that someone help me and says: "There is a problem, remove this line, put this code there."
Then you might try reading that help when it's provided because that's what I did. You don't need any programming experience to know the difference between the beginning and the end of a list.
the reason that your code only removes one item is that you Exit Sub as soon as you remove the first item.
loop from the end to the beginning rather than the beginning to the end.
The outer loop needs to go backwards. The inner loop should also go backwards
When you remove an item, use the loop counter from the outer loop as the index, NOT the inner loop counter.
 
Then you might try reading that help when it's provided because that's what I did. You don't need any programming experience to know the difference between the beginning and the end of a list.

Hello jmcilhinney,
At first, sorry for my earlier reactions. I was disappointed because I was looking for an answer for a week and I became tired. I modified the code by your instructions and it works.
Thank you very much for your help.
VB.NET:
       For intI As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
            For intJ As Integer = DataGridView1.Rows.Count - 1 To intI + 1 Step -1
                If DataGridView1.Rows(intI).Cells(0).Value = DataGridView1.Rows(intJ).Cells(0).Value Then
                    DataGridView1.Rows.RemoveAt(intJ)
                End If
            Next intJ
        Next intI
 
All's well that ends well. :) I know that it can get frustrating when things don't work but just remember that we are total strangers volunteering our time to help you. The onus is on you to make it as easy as you can for us to provide that help and to make the most of it when it's provided. Some people will simply post code that you can copy and paste while others will point you in the right direction and expect you to do some work with those directions and develop enough understanding to write the code for yourself. I fall into the second category and you have demonstrated that, if you take the time, you can develop that understanding. That is a bit harder but it's more rewarding in the long run.
 
All's well that ends well. :) I know that it can get frustrating when things don't work but just remember that we are total strangers volunteering our time to help you. The onus is on you to make it as easy as you can for us to provide that help and to make the most of it when it's provided. Some people will simply post code that you can copy and paste while others will point you in the right direction and expect you to do some work with those directions and develop enough understanding to write the code for yourself. I fall into the second category and you have demonstrated that, if you take the time, you can develop that understanding. That is a bit harder but it's more rewarding in the long run.

I appreciate your work. There is a lot of problems that I need to resolve during programing my project, so I will start new topics.

Thanks a lot once again. :)
 
Back
Top