Moving Records Up and Down

Sriram300

New member
Joined
Jan 4, 2008
Messages
2
Programming Experience
10+
Hello,
Here is the scenario in VB.net windows app.
A text column in Dataset.Datatable is bounded to a listbox. the rows in the table contains, text field1, textfield2, and numeric field represents the order of the rows.

Example

A1 A2 0
B1 B3 1
C1 C2 2
D1 D2 3
.
.
.
My questions is, dow do I manipulate the up/Dn functionality.

I am using the following, It does not work as expected. It's very urgent, Any light on this would be greatly appreciated.

VB.NET:
        If iSelectedIndex > 0 Then
            iSelectedPosId = DS.Tables("Letter").Rows(iSelectedIndex).Item("PosId")
            DS.Tables("Letter").Rows(iSelectedIndex - 1).Item("PosId") = iSelectedPosId
            DS.Tables("Letter").Rows(iSelectedIndex).Item("PosId") = iSelectedPosId - 1
            lbSent.SelectedIndex = lbSent.SelectedIndex - 1
        End If

or

VB.NET:
        If iSelectedIndex > 0 Then
            Dim dr As DataRow
            dr = DS.Tables("Letter").Rows(iSelectedIndex)

            Dim newDR As DataRow
            newDR = DS.Tables("Letter").NewRow
            newDR.ItemArray = dr.ItemArray
            DS.Tables("Letter").Rows.RemoveAt(iSelectedIndex)
            DS.Tables("Letter").Rows.InsertAt(newDR, iSelectedIndex - 1)
            'lbSent.SelectedIndex = lbSent.SelectedIndex - 1

        End If

none of this code works, Please help.
 
Last edited by a moderator:
Your problem is that the rows in the DataTable never actually change their order when you sort. It's the rows in the DefaultView that get sorted. Instead of indexing the Rows property of the table you need to index its DefaultView property:
VB.NET:
iSelectedPosId = DS.Tables("Letter").[B][U]DefaultView[/U][/B](iSelectedIndex).Item("PosId")
Etc., etc.
 
Finally it Worked...!

Your problem is that the rows in the DataTable never actually change their order when you sort. It's the rows in the DefaultView that get sorted. Instead of indexing the Rows property of the table you need to index its DefaultView property:
VB.NET:
iSelectedPosId = DS.Tables("Letter").[B][U]DefaultView[/U][/B](iSelectedIndex).Item("PosId")
Etc., etc.

Thank you all for the help... sorting default view on the datatable worked.

Here is the code...it could help somebody else too...

VB.NET:
   Private Sub btnPosUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPosUp.Click
        Dim iSelectedIndex As Integer
        iSelectedIndex = lbSent.SelectedIndex
        If iSelectedIndex <= 0 Then
            Exit Sub
        End If

        Dim newDR As DataRow
        newDR = DS.Tables("TableName").NewRow

        Dim dr As DataRow
        dr = DS.Tables("TableName").Rows(iSelectedIndex)

        newDR.ItemArray = dr.ItemArray
        DS.Tables("TableName").Rows.RemoveAt(iSelectedIndex)
        DS.Tables("TableName").Rows.InsertAt(newDR, iSelectedIndex - 1)

        Dim drr As DataRow
        Dim i As Integer = 0
        For Each drr In DS.Tables("TableName").Rows
            drr.Item("PosId") = i
            i = i + 1
        Next
        DS.Tables("TableName").DefaultView.Sort = String.Format("OrderCol", "ASC")
        lbSent.SelectedIndex = iSelectedIndex - 1

    End Sub
 
Last edited by a moderator:
Back
Top