dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] Datagridview move cell value up/down

  1. #1

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Resolved [RESOLVED] Datagridview move cell value up/down

    Hi,

    I have been searching high and low but am not really find the answer to my exact scenario and my other thread about drag and drop is probably going to run into another dead end as well..So I am try to think of an alternative.

    I have a table that is bound to a data source.

    - ContextID
    - RiskID
    - RiskNumber
    - Event

    I want to move the rows up and down in the datagridview. What I do is to "sort" based on "RiskNumber" - So if I chage the RiskNumber, the datagridview will automatically sort accordingly.

    So if I click the button "UP" it must basically swap the current RiskNumber with the RiskNumber above it. When I click DOWN button it must swap the current RiskNumber with the RiskNumber below.

    If I select a bottom row and keep on clicking "UP" it must keep on doing that (swapping) untill it reach the top of the DataGridview....

    I cannot figure out how to do this....

    I can get the current row RiskNumber:


    Code:
    Dim currentRiskNumber As String = TblRiskAssessmentDataGridView.Item("DataGridViewTextBoxColumn7", TblRiskAssessmentDataGridView.CurrentRow.Index).Value

    I can get RiskNumber above:

    Code:
    Dim RiskNumberAbove As String = TblRiskAssessmentDataGridView.Item("DataGridViewTextBoxColumn7", TblRiskAssessmentDataGridView.CurrentRow.Index - 1).Value
    I can get RiskNumber below:

    Code:
    Dim RiskNumberBelow As String = TblRiskAssessmentDataGridView.Item("DataGridViewTextBoxColumn7", TblRiskAssessmentDataGridView.CurrentRow.Index + 1).Value
    I cannot figure out how to swop it.....
    Last edited by schoemr; Aug 23rd, 2019 at 05:53 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Datagridview move cell value up/down

    If the .Value property is writeable, you can do this:
    Code:
    TblRiskAssessmentDataGridView.Item(...).Value = newValue
    By the way, as the .Value property is probably not a String, you should use the more generic Object data type for those variables.

  3. #3

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Re: Datagridview move cell value up/down

    Thank you very much SI

    So this is what I have done. Maybe some day this can help someone. I know there will probably be better ways but this works. Also I still have to figure out what to do if the datagridview row reach the top / bottom row (to prevent out of range exception)

    This is to move a cell value down:

    Code:
     'Get the current risk number
            Dim currentRiskNumber As Integer = TblRiskAssessmentDataGridView.Item("DataGridViewTextBoxColumn7", TblRiskAssessmentDataGridView.CurrentRow.Index).Value
            
    
    'Get the risk number below
            Dim riskNumberBelow As Integer = TblRiskAssessmentDataGridView.Item("DataGridViewTextBoxColumn7", TblRiskAssessmentDataGridView.CurrentRow.Index + 1).Value
    
            
    'Set the current risk number to the risk number below
            Me.TblRiskAssessmentDataGridView.Rows(currentRiskNumber - 1).Cells(2).Value = riskNumberBelow
    
           
     'Set the risk number below to the risk number above
            Me.TblRiskAssessmentDataGridView.Rows(RiskNumberBelow - 1).Cells(2).Value = currentRiskNumber
    
            
    'Get the current row position
            Dim currentPosition = TblRiskAssessmentBindingSource.Position
          
       
           
     'Unselect the current row 
            TblRiskAssessmentDataGridView.ClearSelection()
          
    
            'Sort 
            Me.TblRiskAssessmentDataGridView.Sort(TblRiskAssessmentDataGridView.Columns(2), System.ComponentModel.ListSortDirection.Ascending)
    
            'Select the new row (row below) - so that you move along as you click
            TblRiskAssessmentBindingSource.Position = currentPosition + 1

    Also, the datagridview is supposed to sort automatically (and it is not) because I have ORDER BY clause. So I still have to figure out why. That is why I in this example above do the sort programatically.

    Code:
    SELECT        RiskID, ContextID, RiskNumber, Event
    FROM            tblRiskAssessment
    WHERE        (ContextID = @ContextID)
    ORDER BY RiskNumber
    Last edited by schoemr; Aug 23rd, 2019 at 07:38 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Datagridview move cell value up/down

    Your use of currentRiskNumber and RiskNumberBelow to index Rows() makes assumptions about the data that might not always be true (such as if a bug occurs, or requirements change later).

    It would be better to use CurrentRow.Index instead, which can also make the code a bit more readable, eg:
    Code:
            Dim currentIndex As Integer = Me.TblRiskAssessmentDataGridView.CurrentRow.Index
    
     'Get the risk number from the current row
            Dim currentRiskNumber As Object = Me.TblRiskAssessmentDataGridView.Rows(currentIndex).Cells(2).Value
    
     'Copy the value from the other row to the current row
            Me.TblRiskAssessmentDataGridView.Rows(currentIndex).Cells(2).Value = Me.TblRiskAssessmentDataGridView.Rows(currentIndex - 1).Cells(2).Value
    
     'Paste the original value of the current row to the other row
            Me.TblRiskAssessmentDataGridView.Rows(currentIndex - 1).Cells(2).Value = currentRiskNumber
    If you want, you can also use a With block to make the code easier to read (anything inside the With block that starts with a dot refers to the With object):
    Code:
            With Me.TblRiskAssessmentDataGridView
    
              Dim currentIndex As Integer = .CurrentRow.Index
    
     'Get the risk number from the current row
              Dim currentRiskNumber As Object = .Rows(currentIndex).Cells(2).Value
    
     'Copy the value from the other row to the current row
              .Rows(currentIndex).Cells(2).Value = .Rows(currentIndex - 1).Cells(2).Value
    
     'Paste the original value of the current row to the other row
              .Rows(currentIndex - 1).Cells(2).Value = currentRiskNumber 
    
            End With
    I don't know why it isn't sorting automatically (it's been a long time since I did WinForms), but hopefully somebody else will be able to help with that.
    Last edited by si_the_geek; Aug 23rd, 2019 at 08:08 AM. Reason: typo

  5. #5

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Re: Datagridview move cell value up/down

    Hi

    I am getting a Index out of range exception here:

    Code:
      Me.TblRiskAssessmentDataGridView.Rows(currentIndex).Cells(2).Value = Me.TblRiskAssessmentDataGridView.Rows(currentIndex - 1).Cells(2).Value
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Datagridview move cell value up/down

    What is the value of currentIndex at the time it happens?

    I suspect it is 0, so currentIndex - 1 wont be a valid row index.

    To avoid that, don't allow moving up when the current row index is 0 (and no moving down when it is equal to Rows.Count - 1).

  7. #7
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,166

    Re: Datagridview move cell value up/down

    Quote Originally Posted by schoemr View Post
    Also I still have to figure out what to do if the datagridview row reach the top / bottom row (to prevent out of range exception)
    Simply check for that condition first. If they select down and they are already on the bottom row, Exit Sub.

    Also, the datagridview is supposed to sort automatically (and it is not) because I have ORDER BY clause. So I still have to figure out why. That is why I in this example above do the sort programatically.
    It seems your code is modifying the DGV values, but that is not updating the underlying datasource. I usually do things manually, so I would be modifying the datasource and then refresh the DGV.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    Re: Datagridview move cell value up/down

    Hello,

    Another idea is to work against a int column which not only permits ordering via up/down buttons but also remembers the order when showing the DataGridView each time.

    https://code.msdn.microsoft.com/Move...7fe786?redir=0

    Name:  DataGridview.jpg
Views: 42
Size:  36.2 KB

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    Re: Datagridview move cell value up/down

    Hello,

    Another idea is to work against a int column which not only permits ordering via up/down buttons but also remembers the order when showing the DataGridView each time.

    https://code.msdn.microsoft.com/Move...7fe786?redir=0

    Name:  DataGridview.jpg
Views: 42
Size:  36.2 KB

  10. #10

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Re: Datagridview move cell value up/down

    Hello Karen,

    Thank you for your reply. I actually find this work you did some time ago when I was searching on how to do this. But I assumed there something wrong with the download because I could not load it:

    Name:  5.JPG
Views: 38
Size:  26.6 KB
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  11. #11

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Re: Datagridview move cell value up/down

    So manage to make it work, but I can not make it to keep on working!!!!!!


    I want to move "a" down to bottom:

    Name:  1.JPG
Views: 37
Size:  29.7 KB

    Then after 3 clicks "a" is at the bottom:

    Name:  2.JPG
Views: 36
Size:  30.0 KB

    At this point it is worked as the way I want. But now I want to move "b" down:

    Name:  3.JPG
Views: 37
Size:  30.0 KB

    It only gets to here and then no matter how many time I click it stays here:

    Name:  4.JPG
Views: 37
Size:  30.2 KB




    Code:
         'Get current index
            Dim currentIndex As Integer = Me.TblRiskAssessmentDataGridView.CurrentRow.Index
            'Get the risk number from the current row
            Dim currentRiskNumber As Object = Me.TblRiskAssessmentDataGridView.Rows(currentIndex).Cells(2).Value
    
            'Get the risk number from the row below
            Dim RiskNumberBelow As Object = Me.TblRiskAssessmentDataGridView.Rows(currentIndex + 1).Cells(2).Value
    
    
            'Swap
    
    
            MsgBox("Risk Number below: " & RiskNumberBelow & vbCrLf & "Current Risk Number: " & currentRiskNumber & vbCrLf & "Current Index: " & currentIndex)
    
            Me.TblRiskAssessmentDataGridView.Rows(currentIndex + 1).Cells(2).Value = currentRiskNumber
            Me.TblRiskAssessmentDataGridView.Rows(currentIndex).Cells(2).Value = RiskNumberBelow
    
            TblRiskAssessmentBindingSource.EndEdit()
    
            MsgBox("Risk Number below: " & RiskNumberBelow & vbCrLf & "Current Risk Number: " & currentRiskNumber & vbCrLf & "Current Index: " & currentIndex)
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Datagridview move cell value up/down

    Do you get the correct amount of MsgBox's?

    Do they show the values you would expect every time?

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    Re: Datagridview move cell value up/down

    Quote Originally Posted by schoemr View Post
    Hello Karen,

    Thank you for your reply. I actually find this work you did some time ago when I was searching on how to do this. But I assumed there something wrong with the download because I could not load it:

    Name:  5.JPG
Views: 38
Size:  26.6 KB
    Been away all day, just did a download without any issues extracting the solution. I just placed the solution on GitHub.

    https://github.com/karenpayneoregon/...wsUpDown_VBNET

  14. #14
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    Re: Datagridview move cell value up/down

    Quote Originally Posted by schoemr View Post
    Hello Karen,

    Thank you for your reply. I actually find this work you did some time ago when I was searching on how to do this. But I assumed there something wrong with the download because I could not load it:

    Name:  5.JPG
Views: 38
Size:  26.6 KB
    Been away all day, just did a download without any issues extracting the solution. I just placed the solution on GitHub.

    https://github.com/karenpayneoregon/...wsUpDown_VBNET

  15. #15

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,078

    Re: Datagridview move cell value up/down

    Quote Originally Posted by kareninstructor View Post
    Been away all day, just did a download without any issues extracting the solution. I just placed the solution on GitHub.

    https://github.com/karenpayneoregon/...wsUpDown_VBNET
    Hi Karen,

    Thank you so much
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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