Re-ordering ListBox items bound to a DataView
I have a ListBox that is bound to a DataView. The DataView (called componentsView) has the Sort property set to sort by "Pos", a Byte-type, unique-constrained column. Buttons are used to add or remove items to or from the ListBox. Upon adding items to the ListBox, the first item is given a "Pos" value of 1 and the second a "Pos" value of 2 and so on.
My goal now is to implement buttons that are used to move items up or down the ListBox by one position. I intend to do this by modifying the value of the "Pos" column which will cause the ListBox to reorder according to the Sort property. The code below shows how I am currently trying to do that:
However, the problem arises at the last line of code below executes which throws a ConstraintException (the compiler says, "Column 'Pos' is constrained to be unique. Value '2' is already present."). I do not know why '2' is still present because I changed the "Pos" column value that was originally '2' to '255'.
Dim rowToMoveUp As DataRowView = componentsView.Item(ComponentsListBox.SelectedIndex)
Dim rowToMoveDown As DataRowView = componentsView.Item(ComponentsListBox.SelectedIndex - 1)
Dim upperPos As Byte = CByte(rowToMoveDown.Row.Item("Pos"))
Dim lowerPos As Byte = CByte(rowToMoveUp.Row.Item("Pos"))
rowToMoveDown.Row.Item("Pos") = 255
rowToMoveUp.Row.Item("Pos") = upperPos
rowToMoveDown.Row.Item("Pos") = lowerPos
Can anyone explain why this is happening?
This may not be the best solution to address the proposition anyway so I would be grateful for any alternative suggestions.
Last Post: 08-19-2002, 12:47 PM
By Dean Earley in forum VB Classic
Last Post: 06-14-2002, 12:55 PM
Last Post: 04-15-2002, 02:05 PM
Last Post: 02-21-2002, 08:00 AM
Last Post: 09-18-2000, 05:04 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center