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.