Results 1 to 2 of 2

Thread: [RESOLVED] Maintaining selected items in DGV

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2020
    Posts
    35

    Resolved [RESOLVED] Maintaining selected items in DGV

    In my Winform application, I have a DataGridView where a user can select options from the list. There may be one option, there may be 20.
    The DGV is multiselect and due to design constrains shows 4 items and can scroll vertically. It is purely for display and selection of options

    Ive implemented code which maintains items as selected when using the mouse.
    ie, when a user record is edited, it shows a DGV with prepopulated options.
    The user can add / remove selections without holding the CTRL button

    So basically for ease of use, Im allowing the user to select / remove with just the mouse.

    The code sample is from here:
    https://stackoverflow.com/questions/...n-datagridview

    This works perfectly!

    However, it only works when all of the rows of the DGV are visible, ie if they are scrolled out of view, it no longer keeps those rows selected when saved.

    As per image, if all rows are selected (and visible), all rows are saved
    Name:  image1.png
Views: 98
Size:  5.5 KB

    When the design height of the DGV is shortened this is the result:
    Name:  image2.png
Views: 93
Size:  4.5 KB
    All items can be selected, however only the visible items are saved. eg, if you select all options, only options 1,2,3 are saved even though the row is selected.

    Name:  image3.png
Views: 93
Size:  5.4 KB

    Code:
    Public intSelectedRows As New List(Of Integer)
    
        Private Sub DataGridView_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView.RowPostPaint
    
            If Me.intSelectedRows.Contains(e.RowIndex) Then
                CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
            End If
    
        End Sub
    
    Private Sub DataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellClick
    
            With CType(sender, DataGridView)
                Dim intRow As Integer = .CurrentRow.Index
                If Not Me.intSelectedRows.Contains(intRow) Then
                    Me.intSelectedRows.Add(intRow)
                Else
                    .CurrentRow.Selected = False
                    Me.intSelectedRows.Remove(intRow)
                End If
            End With
    
           LabelActCount.Text = "(" & intSelectedRows.Count & " of " & DataGridView.Rows.Count & ")"
    
        End Sub
    Any advice or better options?

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2020
    Posts
    35

    Re: [RESOLVED] Maintaining selected items in DGV

    Stand down, resolved. Save was looking for a selected row, not the List integer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width