all groups > dotnet windows forms databinding > september 2006 >
You're in the

dotnet windows forms databinding

group:

Moving items within a ListBox/DataView



Moving items within a ListBox/DataView SparkPlug
9/15/2006 7:39:02 AM
dotnet windows forms databinding: I am writing an application (code snippet below) that contains a ListBox
control bound to a DataView (consisting of a single DataTable which is part
of larger DataSet).

The user should be able to click on buttons that will move list items either
up or down in the order that they appear in the ListBox. The DataView items,
(and,thus, the items in the ListBox) are ordered according to a Sort property
value set on a specific column that is constrained as unique.

In the code, items are moved up or down by changing the data value in the
"sort column" thus causing the DataView to automatically re-sort itself
according to the Sort property value.

However, when the sort takes place it appears to cause the row references
from the DataView to the DataTable to become "out-of-sync" with each other;
DataRowViews in the DataView end up pointing to the wrong DataRows in the
DataTable.

How can I correct this error in the existing code or, if that is not
possible, rewrite the code to achieve the same effect using a ListBox or
suitable alternative control? What is the underlying behaviour of .NET that
is causing this and what is the rationale for it (i.e. is it a bug or a
feature)?

Code for moving an item up:

Private Sub MoveUpButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MoveUpButton.Click

If Not Me.MyListBox.SelectedIndex = 0 And _
Not Me.MyListBox.SelectedIndex = -1 Then

Dim rowToMoveUp As DataRowView =
selectedComponentsView.Item(MyListBox.SelectedIndex)
Dim rowToMoveDown As DataRowView =
selectedComponentsView.Item(MyListBox.SelectedIndex - 1)
Dim upperPosition As Byte =
CByte(rowToMoveDown.Row.Item("OrderPosition"))
Dim lowerPosition As Byte =
CByte(rowToMoveUp.Row.Item("OrderPosition"))

rowToMoveDown.Row.Item("OrderPosition") = 255
rowToMoveUp.Row.Item("OrderPosition") = upperPosition
rowToMoveDown.Row.Item("OrderPosition") = lowerPosition

End If
End Sub



I look forward to receiving your comments.

Many thanks
Mark
Re: Moving items within a ListBox/DataView Bart Mermuys
9/17/2006 7:06:46 PM
Hi,

[quoted text, click to view]

In NET1.1 this seems to work, works better if you reverse the swapping (not
sure why):
rowToMoveUp.Row("OrderPosition") = 255
rowToMoveDown.Row("OrderPosition") = lowerPosition
rowToMoveUp.Row("OrderPosition") = upperPosition

In NET2.0 this won't work, because the DataRowView has changed. The
DataRowView's are more like indexes into the DataView, once the DataView
changes (eg. the first move), the same DataRowView(same index) may refer to
another DataRow. So you need to grab the DataRows instead of the
DataRowViews:

Dim rowToMoveUp As DataRow =
selectedComponentsView.Item(MyListBox.SelectedIndex).Row
Dim rowToMoveDown As DataRow =
selectedComponentsView.Item(MyListBox.SelectedIndex - 1).Row

Dim upperPosition As Byte = CByte(rowToMoveDown("OrderPosition"))
Dim lowerPosition As Byte = CByte(rowToMoveUp("OrderPosition"))

rowToMoveUp("OrderPosition") = 255
rowToMoveDown("OrderPosition") = lowerPosition
rowToMoveUp"OrderPosition") = upperPosition

---
HTH,
Greetings


[quoted text, click to view]

AddThis Social Bookmark Button