dcsimg
Results 1 to 6 of 6

Thread: How to sort dataset and save as xml

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    How to sort dataset and save as xml

    Hi

    I have xml file and try to load into datagridview, how can I save the xml after I sort it by desc?

    Code:
    DS.ReadXml(Application.StartupPath & "\user.xml")
            Dim DS As New DataSet
            If DS.Tables(0).Rows.Count = 0 Then
                MsgBox("nothing")
            Else
                Dim i As Integer = 0
                For Each r As DataRow In DS.Tables(0).Rows
                    dtgUser.Rows.Insert(i, r("ID"), r("Name"))
                    i = i + 1
                Next
            End If
            dtgUser.Sort(dtgUser.Columns("ID"), System.ComponentModel.ListSortDirection.Ascending)
    
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
               
            DS.WriteXml(Application.StartupPath & "\user.xml")
                
        End Sub

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to sort dataset and save as xml

    In the example code you've given DS is out of scope in the btnAdd_Click method. You'll want to sort the DataTable's DefaultView and then write the xml out.

    Hand written so there may be some syntax errors.

    Code:
    DS.Tables(0).DefaultView.Sort = "ID ASC"
    Dim outputDataTable = DS.Tables(0).DefaultView.ToTable()
    outputDataTable.WriteXml("c:\blah\user.xml")

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: How to sort dataset and save as xml

    Quote Originally Posted by MattP View Post
    In the example code you've given DS is out of scope in the btnAdd_Click method. You'll want to sort the DataTable's DefaultView and then write the xml out.

    Hand written so there may be some syntax errors.

    Code:
    DS.Tables(0).DefaultView.Sort = "ID ASC"
    Dim outputDataTable = DS.Tables(0).DefaultView.ToTable()
    outputDataTable.WriteXml("c:\blah\user.xml")
    But when I select the row after I sort, the selected row data show in textbox is different from datagridview

    Private Sub dtgUser_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgUser.SelectionChanged
    Try

    Dim rows As DataRow = DS.Tables(0).Rows(dtgUser.CurrentRow.Index)

    ID.Text = rows("ID")
    name.Text = rows("Name")
    Catch ex As Exception

    End Try

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,890

    Re: How to sort dataset and save as xml

    You aren't sorting the order of the rows in the table, you are sorting the order of the rows in a view taken from the table. The table hasn't sorted, only the view has. Therefore, the rows in the table are still the same as they were, but the index is the index of the row in the dataview bound to the table, which HAS been sorted, so row N in the dataview is no longer the same as row N in the datatable.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: How to sort dataset and save as xml

    Quote Originally Posted by Shaggy Hiker View Post
    You aren't sorting the order of the rows in the table, you are sorting the order of the rows in a view taken from the table. The table hasn't sorted, only the view has. Therefore, the rows in the table are still the same as they were, but the index is the index of the row in the dataview bound to the table, which HAS been sorted, so row N in the dataview is no longer the same as row N in the datatable.
    So may I know how to sort the table instead of datagridview?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,890

    Re: How to sort dataset and save as xml

    You don't.

    Don't worry about the order of the rows in the tables, as that makes not a bit of difference to anybody. All the data is seen through a view of some sort, and that view can be sorted or restricted in any way you choose. Putting the rows from the table into some kind of order doesn't benefit anybody, and would be pretty difficult. What you would be doing would be sorting the rows in the Rows collection, and you would have to provide a custom comparator to do so. This may not even be possible (I've never tried, and see no point in it), in which case you'd have to create a new datatable with the same schema, then hand sort the rows into the new table.

    Just don't bother. Let the data be in whatever form it is, and display it in whatever form you choose.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width