-
Apr 16th, 2021, 07:29 PM
#1
Thread Starter
Member
[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
When the design height of the DGV is shortened this is the result:
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.
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?
-
Apr 16th, 2021, 07:56 PM
#2
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|