VS 2015 [RESOLVED] multiselect cells in a datagridview and set value's of checkboxes-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] multiselect cells in a datagridview and set value's of checkboxes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2017
    Location
    Netherlands
    Posts
    85

    Resolved [RESOLVED] multiselect cells in a datagridview and set value's of checkboxes

    Good day

    i have a piece of code that when it selects multiple cells in a datgridview that it changes the value's to true and if viceversa
    code:
    Code:
    Private Sub RestHours2DataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles RestHours2DataGridView.SelectionChanged
            For Each selectedCell As DataGridViewCell In DirectCast(Me.RestHours2DataGridView.SelectedCells, BaseCollection)
                If CBool(selectedCell.Value) = True Then
                    selectedCell.Value = False
                ElseIf CBool(selectedCell.Value) = False Then
                    selectedCell.Value = True
                End If
            Next
        End Sub
    When i have all cells are true and i select the cells again they not all turn to false, but only partialy, if i select them one by one there is no problem but with multiselect i get like below right picture which leaves some cells in checked condition
    Name:  1.jpg
Views: 151
Size:  28.6 KB Name:  2.jpg
Views: 151
Size:  33.1 KB

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,079

    Re: multiselect cells in a datagridview and set value's of checkboxes

    Firstly, this:
    vb.net Code:
    1. If CBool(selectedCell.Value) = True Then
    2.     selectedCell.Value = False
    3. ElseIf CBool(selectedCell.Value) = False Then
    4.     selectedCell.Value = True
    5. End If
    is an extremely verbose way to toggle a Boolean. All you need is this:
    vb.net Code:
    1. selectedCell.Value = Not CBool(selectedCell.Value)
    As for your issue, it doesn't seem appropriate to toggling check boxes in the SelectionChanged event handler. Think about what is going to happen. Let's say that you have every box unchecked initially. When you select the first cell, that box will be checked. When you select the second cell, the event is raised again so the new cell will be checked but the first one will be unchecked. When you select the third cell, it will be checked, the second one will be unchecked and the first will be checked again. It doesn't make sense to be making changes as you select. You should select all the cells you want to toggle first, then click a Button to toggle them all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2017
    Location
    Netherlands
    Posts
    85

    Re: multiselect cells in a datagridview and set value's of checkboxes

    Yes you are right was thinking allready that it is checking all selected cells again, but i would like to make it without having to click a button, could do it in a mouse click offcourse

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,079

    Re: multiselect cells in a datagridview and set value's of checkboxes

    You can't do it without a Button or something similar. We've already established that you can't toggle the cells as you select them; you have to wait until you've selected them all first. How is your software supposed to know that you've finished selecting? It can't read your mind so you need to perform some action, e.g. clicking a Button. You could potentially use a Timer and do it automatically when a certain amount of time passes after the last selection change but you'd have to make that time long enough to accommodate slow people and that would annoy fast people who would then have to wait.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2017
    Location
    Netherlands
    Posts
    85

    Re: multiselect cells in a datagridview and set value's of checkboxes

    Like said i put it in mouseclick event, click select and let go and it is setting to true where is false and vice versa
    have not seen any strange things yet so it is working as i can see now.
    Thanks for the guidance

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.