I need some advice on how to walk a row up and down a grid by changing it's rank value with either of two buttons.
I'm using VS2008.
I have a DataGridView which is working with data from a SQL 2005 database. The grid has 2 columns and 0-n rows. Column 0 holds a string which represents, say, a color. Column 2 holds an integer which represents the rank of the associated color on a scale of 1 to (# of colors). The rank value for each color must be unique and all rank values must be accounted for.
I have the grid set to sort automatically on Col2, so that the list of colors will always be sorted from Most Favorite(1) to Least Favorite(# of colors). I have the selection mode set to Full Row.
To the right of the grid there are two buttons: Up and Down. Clicking Up will raise (decrement) the rank of the color, and clicking Down will lower (increment) the rank. As the user clicks either button, they should see the selected item moving up and down in the grid.
So, if they click up, I get the rank value of the selected color, and that of the color whose position the selected color will occupy. I then swap their values, providing they validate (such as not already being the topmost position, etc.)
The problem I'm having is that when I change the selected row's rank value, the grid seems to be resorting instantly, so that my next line, which changes the adjacent color's rank value, becomes invalidated. What ends up happening is that I effectively change the rank value and then immediately change it back.
Here's my code. I have yet to generalize it to a function, so I'll just show both buttons' methods for completeness.
Thanks in advance.
I'm using VS2008.
I have a DataGridView which is working with data from a SQL 2005 database. The grid has 2 columns and 0-n rows. Column 0 holds a string which represents, say, a color. Column 2 holds an integer which represents the rank of the associated color on a scale of 1 to (# of colors). The rank value for each color must be unique and all rank values must be accounted for.
I have the grid set to sort automatically on Col2, so that the list of colors will always be sorted from Most Favorite(1) to Least Favorite(# of colors). I have the selection mode set to Full Row.
To the right of the grid there are two buttons: Up and Down. Clicking Up will raise (decrement) the rank of the color, and clicking Down will lower (increment) the rank. As the user clicks either button, they should see the selected item moving up and down in the grid.
So, if they click up, I get the rank value of the selected color, and that of the color whose position the selected color will occupy. I then swap their values, providing they validate (such as not already being the topmost position, etc.)
The problem I'm having is that when I change the selected row's rank value, the grid seems to be resorting instantly, so that my next line, which changes the adjacent color's rank value, becomes invalidated. What ends up happening is that I effectively change the rank value and then immediately change it back.
Here's my code. I have yet to generalize it to a function, so I'll just show both buttons' methods for completeness.
VB.NET:
' At a higher scope:
Dim cr as integer = gvw_Ranks.SelectedRows.Index(0)
'...
Private Sub gvw_Ranks_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvw_Ranks.CellClick
cr = gvw_Ranks.SelectedRows(0).Index
End Sub
Private Sub btn_RankUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_RankUp.Click
cr = gvw_Ranks.SelectedRows(0).Index
Dim rr As Object = gvw_Ranks.Item(1, cr)
If rr.Value > 1 Then
Dim rp As Object = gvw_Ranks.Item(1, cr - 1)
rr.Value -= 1
rp.Value += 1
End If
End Sub
Private Sub btn_RankDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_RankDown.Click
cr = gvw_Ranks.SelectedRows(0).Index
Dim rr As Object = gvw_Ranks.Item(1, cr)
If rr.Value < gvw_Ranks.Rows.Count() Then
Dim rn As Object = gvw_Ranks.Item(1, cr + 1)
rr.Value += 1
rn.Value -= 1
End If
End Sub
Thanks in advance.
Last edited: