none
Add line if not present into datagridview RRS feed

  • Question

  • I have the below code, it works if I pre-populate the dgview_totals datagridview with the Make, Model and Bios and rem out the dgview_totals.Row.Add line, if I don't I get a ton of repetitive rows that don't add up correctly. ( I have changed the identifier names for work related reasons but am sure they match up correctly).

    if not string.isnullorempty(DeviceName) and not string.isnullorempty(Manufacturer) Then
    dgview_01.Rows.Add({UCase(DeviceName),Manufacturer,Model, BiosInfo,DomainRole})
    
    For i as Integer = 0 to dgview_totals.Rows.Count -1
    If dgview_totals.Rows(i).cells("col_Manufacturer").Value = Manufacturer AND dgview_totals.Rows(i).cells("col_Model").Value = Model AND dgview_totals.Rows(i).cells("col_BiosInfo").Value THEN
    dgview_totals.Rows(i).cells("col_Quantity").Value = dgview_totals.Rows(i).cells("col_Quantity").Value + 1
    Else
    dgview_totals.Rows.Add(1,Manufacturer,Model, BiosInfo)
    End if
    dgview_totals.Refresh()
    Next
    End if

    Wednesday, February 13, 2019 2:41 PM

All replies

  • Hello,

    Take a screenshot, present it here and describe the problem visually please.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 13, 2019 4:54 PM
    Moderator
  • :-( Sorry Karen I can't do that, our systems are separate from the internet.

    Wednesday, February 13, 2019 5:03 PM
  • :-( Sorry Karen I can't do that, our systems are separate from the internet.

    Okay

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 13, 2019 5:10 PM
    Moderator
  • Hi,

    I think you need to describe your issue in detail so we can assist you.

    Best Regards,

    Alex


    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.

    Thursday, February 14, 2019 8:40 AM
  • I have two datagridviews, one holds the scan data of a network for all PC info found. If a device is found the make, model and bios version are inserted into the second table. I am trying to get them to group without having redundant information, so each model only shows once with a quantity field that increases as the model is found again.

    • Edited by Ryan Lashway Thursday, February 14, 2019 2:25 PM
    Thursday, February 14, 2019 2:23 PM
  • Hi,

    I still can't understand what you mean,But it seems to be related to the DataGridview to see if there is a duplicate,such as the second column check.

    Dim t = 1
            If Me.DataGridView1.Rows.Count > 1 AndAlso e.RowIndex > 0 Then
                If e.ColumnIndex = t Then
                    For i As Integer = 0 To e.RowIndex - 1
                        If Me.DataGridView1.Rows(i).Cells(t).Value.ToString() = Me.DataGridView1.Rows(e.RowIndex).Cells(t).Value.ToString() Then
                            MessageBox.Show("The value is repeated", "Warning")
                            Exit For
                        End If
                    Next
                End If
            End If

    Best Regards,

    Alex


    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.


    • Edited by Alex Li-MSFT Wednesday, February 20, 2019 9:49 AM
    Wednesday, February 20, 2019 9:49 AM
  • Unsure if this is what you mean by grouping.

    Originally done in C# here, converted to VB.NET (see demo)

    Public Class GroupByGrid
        Inherits DataGridView
    
        Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
            MyBase.OnCellFormatting(args)
    
            ' First row always displays
            If args.RowIndex = 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.Value = String.Empty
                args.FormattingApplied = True
            End If
        End Sub
        Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal colIndex As Integer) As Boolean
            Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)
            Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)
    
            If (currCell.Value Is prevCell.Value) OrElse (currCell.Value IsNot Nothing AndAlso prevCell.Value IsNot Nothing AndAlso currCell.Value.ToString() = prevCell.Value.ToString()) Then
                Return True
            Else
                Return False
            End If
    
        End Function
        Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)
    
            MyBase.OnCellPainting(args)
    
            args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
            ' Ignore column and row headers and first row
            If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
            Else
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
            End If
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 20, 2019 12:07 PM
    Moderator
  • Sorry, have been sick and just getting back to this.

    What I have is a loop that goes from PC to PC on our network by IP addresses 1-254. in this loop a datagridview is populated with all the information I pull from the win32 values.

    Now as this loop goes I want to add records to another table and have it record the make, model and count of these devices.

    So the main datagridview will have 200 rows with device name, make, model, ram, hd, os, nic info the works, but the second datagridview may only have 5 rows with quantity fields that total up to the 200 rows as we only have 5 models of PC.

    So the flow would be similar to this:

    1. Connect to PC
    2. Scan Info
    3. Add Row DGV01 (make, model, cpu, ram, hd)
    4. Loop thru DGV02 for row where make and model equal the value added to DGV02
    5. If found col_quantity +1
    6. If not found col_quantity = 1, dgview.row.add(quantity, make model)
    Friday, February 22, 2019 3:28 PM
  • Hi,

    How do you hide data in the gridview?

    Best Regards,

    Alex


    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, February 27, 2019 9:52 AM
  • Unsure if this is what you mean by grouping.

    Originally done in C# here, converted to VB.NET (see demo)

    Public Class GroupByGrid
        Inherits DataGridView
    
        Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
            MyBase.OnCellFormatting(args)
    
            ' First row always displays
            If args.RowIndex = 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.Value = String.Empty
                args.FormattingApplied = True
            End If
        End Sub
        Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal colIndex As Integer) As Boolean
            Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)
            Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)
    
            If (currCell.Value Is prevCell.Value) OrElse (currCell.Value IsNot Nothing AndAlso prevCell.Value IsNot Nothing AndAlso currCell.Value.ToString() = prevCell.Value.ToString()) Then
                Return True
            Else
                Return False
            End If
    
        End Function
        Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)
    
            MyBase.OnCellPainting(args)
    
            args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
            ' Ignore column and row headers and first row
            If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
            Else
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
            End If
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi Karen, This is pretty cool.

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Wednesday, February 27, 2019 4:33 PM
  • Thanks for the recommendation Karen, am redoing my machine now but hopefully I can modify this to do something similar to what your code is.

    In your provided code you are grouping by last name, what I am doing is grouping by make and model and increasing a counter. So using what you have, and in reference to my issue, instead of adding the firstname to the last name group, I would have something similar to the following (in two separate columns).

    • 3 - Adams
    • 1 - Alexandra
    • 2 - Anderson
    • 1 - Bell
    • 1 - Brown

    Wednesday, February 27, 2019 6:40 PM