-
Aug 23rd, 2019, 05:42 AM
#1
Thread Starter
Frenzied Member
[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.
-
Aug 23rd, 2019, 06:11 AM
#2
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.
-
Aug 23rd, 2019, 07:27 AM
#3
Thread Starter
Frenzied Member
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.
-
Aug 23rd, 2019, 08:00 AM
#4
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
-
Aug 23rd, 2019, 08:19 AM
#5
Thread Starter
Frenzied Member
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
-
Aug 23rd, 2019, 09:14 AM
#6
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).
-
Aug 23rd, 2019, 09:18 AM
#7
Re: Datagridview move cell value up/down
Originally Posted by schoemr
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.
-
Aug 24th, 2019, 08:21 AM
#8
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
-
Aug 24th, 2019, 08:21 AM
#9
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
-
Aug 24th, 2019, 11:18 AM
#10
Thread Starter
Frenzied Member
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:
Attachment 170645
-
Aug 24th, 2019, 11:28 AM
#11
Thread Starter
Frenzied Member
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:
Attachment 170647
Then after 3 clicks "a" is at the bottom:
Attachment 170649
At this point it is worked as the way I want. But now I want to move "b" down:
Attachment 170651
It only gets to here and then no matter how many time I click it stays here:
Attachment 170653
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)
-
Aug 24th, 2019, 02:05 PM
#12
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?
-
Aug 24th, 2019, 09:04 PM
#13
Re: Datagridview move cell value up/down
Originally Posted by schoemr
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:
Attachment 170645
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
-
Aug 24th, 2019, 09:04 PM
#14
Re: Datagridview move cell value up/down
Originally Posted by schoemr
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:
Attachment 170645
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
-
Aug 26th, 2019, 08:34 AM
#15
Thread Starter
Frenzied Member
Re: Datagridview move cell value up/down
Originally Posted by kareninstructor
Hi Karen,
Thank you so much
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
|