locked
Choose the first 3 repeated elements in the matrix RRS feed

  • Question

  • HI

    I want to choose the first 3 repeated elements inside the matrix with the following condition:

    Each non-repeating row is determined automatically.

    The first 3 duplicate rows are selected and the rest of the duplicate rows are deleted, where the named cell “hide” equal same cell “hide” and the named cell “pr” equal the “pr” for the same row.

    image datagridview 


    list array data

    Dim f_c_productList = New List(Of ProductItem)
    
    
     For index = 0 To f_c_productList.Count - 1
                    ' name = na column  , discount = ds column , Price = pr column ,company = filename column ,hiden_name = hide column
                    DATAG_CLIEN.Rows.Add(f_c_productList(index).Name, f_c_productList(index).Discount, f_c_productList(index).Price, f_c_productList(index).company, f_c_productList(index).hiden_name)
    
                Next


    • Edited by ahmeddc Sunday, September 13, 2020 12:11 PM
    Tuesday, September 8, 2020 11:25 AM

Answers

  • Hi ahmeddc,

    I make a test based on your description, and you can refer to the following code to remove the first 3 duplicate rows in order.

    Public Class Form1
        Private f_c_productList As List(Of ProductItem) = New List(Of ProductItem)()
        Dim contentList As List(Of String) = New List(Of String)()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Columns.Add("hide", "hide")
            DataGridView1.Columns.Add("FILENAME", "FILENAME")
            DataGridView1.Columns.Add("PR", "PR")
            DataGridView1.Columns.Add("DS", "DS")
            DataGridView1.Columns.Add("NA", "NA")
            f_c_productList.AddRange(New ProductItem() {New ProductItem With {
                .Name = "NA1",
                .Discount = 28,
                .company = "FILENAME1",
                .hiden_name = "A",
                .Price = 89.75
            }, New ProductItem With {
                .Name = "NA2",
                .Discount = 39,
                .company = "FILENAME2",
                .hiden_name = "B",
                .Price = 13.5
            }, New ProductItem With {
                .Name = "NA7",
                .Discount = 35,
                .company = "FILENAME7",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA3",
                .Discount = 38,
                .company = "FILENAME3",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA4",
                .Discount = 37,
                .company = "FILENAME4",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA5",
                .Discount = 37,
                .company = "FILENAME5",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA6",
                .Discount = 37,
                .company = "FILENAME6",
                .hiden_name = "B",
                .Price = 21.25
            }})
    
            For i As Integer = 0 To f_c_productList.Count - 1
                DataGridView1.Rows.Add(f_c_productList(i).hiden_name, f_c_productList(i).company, f_c_productList(i).Price, f_c_productList(i).Discount, f_c_productList(i).Name)
            Next
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim query = DataGridView1.Rows.OfType(Of DataGridViewRow)().Where(
                Function(row) Not row.IsNewRow).OrderByDescending(
                Function(x) x.Cells(3).Value).GroupBy(
                Function(row) row.Cells("hide").Value).[Select](
                Function(group) New With {Key .Rows = group.ToList()
            }).ToList()
    
            For Each item In query
                contentList.Clear()
                Dim count As Integer = 0
    
                For i As Integer = 0 To item.Rows.Count - 1
    
                    If contentList.Contains(item.Rows(i).Cells(2).Value.ToString()) Then
                        count += 1
    
                        If count = 3 Then
                            DataGridView1.Rows.Remove(item.Rows(i))
                            count -= 1
                        End If
                    Else
                        contentList.Add(item.Rows(i).Cells(2).Value.ToString())
                    End If
                Next
            Next
        End Sub
    End Class
    Class ProductItem
        Public Property Name As String
        Public Property Discount As Integer
        Public Property Price As Double
        Public Property company As String
        Public Property hiden_name As String
    End Class

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 9, 2020 7:33 AM

All replies

  • Are you pointing to the red box of rows?  Is selecting 3 a constant or what happens if they select 2 or 4 ?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, September 8, 2020 11:46 AM
  • HI karen

    Are you pointing to the red box of rows?  Is selecting 3 a constant or what happens if they select 2 or 4 ?

    Refer to the first box on the non-duplicate elements that remain without deletion, and the second box to the duplicate element with more than 3 elements

    Tuesday, September 8, 2020 12:15 PM
  • Seems like using a linq or lambda query with a group by using columns which will give you distinct items and within duplicates. If interest I can give you an example that you can work into your task.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, September 8, 2020 1:37 PM
  • Hi ahmeddc,

    I make a test based on your description, and you can refer to the following code to remove the first 3 duplicate rows in order.

    Public Class Form1
        Private f_c_productList As List(Of ProductItem) = New List(Of ProductItem)()
        Dim contentList As List(Of String) = New List(Of String)()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Columns.Add("hide", "hide")
            DataGridView1.Columns.Add("FILENAME", "FILENAME")
            DataGridView1.Columns.Add("PR", "PR")
            DataGridView1.Columns.Add("DS", "DS")
            DataGridView1.Columns.Add("NA", "NA")
            f_c_productList.AddRange(New ProductItem() {New ProductItem With {
                .Name = "NA1",
                .Discount = 28,
                .company = "FILENAME1",
                .hiden_name = "A",
                .Price = 89.75
            }, New ProductItem With {
                .Name = "NA2",
                .Discount = 39,
                .company = "FILENAME2",
                .hiden_name = "B",
                .Price = 13.5
            }, New ProductItem With {
                .Name = "NA7",
                .Discount = 35,
                .company = "FILENAME7",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA3",
                .Discount = 38,
                .company = "FILENAME3",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA4",
                .Discount = 37,
                .company = "FILENAME4",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA5",
                .Discount = 37,
                .company = "FILENAME5",
                .hiden_name = "B",
                .Price = 21.25
            }, New ProductItem With {
                .Name = "NA6",
                .Discount = 37,
                .company = "FILENAME6",
                .hiden_name = "B",
                .Price = 21.25
            }})
    
            For i As Integer = 0 To f_c_productList.Count - 1
                DataGridView1.Rows.Add(f_c_productList(i).hiden_name, f_c_productList(i).company, f_c_productList(i).Price, f_c_productList(i).Discount, f_c_productList(i).Name)
            Next
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim query = DataGridView1.Rows.OfType(Of DataGridViewRow)().Where(
                Function(row) Not row.IsNewRow).OrderByDescending(
                Function(x) x.Cells(3).Value).GroupBy(
                Function(row) row.Cells("hide").Value).[Select](
                Function(group) New With {Key .Rows = group.ToList()
            }).ToList()
    
            For Each item In query
                contentList.Clear()
                Dim count As Integer = 0
    
                For i As Integer = 0 To item.Rows.Count - 1
    
                    If contentList.Contains(item.Rows(i).Cells(2).Value.ToString()) Then
                        count += 1
    
                        If count = 3 Then
                            DataGridView1.Rows.Remove(item.Rows(i))
                            count -= 1
                        End If
                    Else
                        contentList.Add(item.Rows(i).Cells(2).Value.ToString())
                    End If
                Next
            Next
        End Sub
    End Class
    Class ProductItem
        Public Property Name As String
        Public Property Discount As Integer
        Public Property Price As Double
        Public Property company As String
        Public Property hiden_name As String
    End Class

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 9, 2020 7:33 AM