-
Feb 23rd, 2012, 11:54 AM
#1
Thread Starter
New Member
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
-
Feb 23rd, 2012, 12:04 PM
#2
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")
-
Feb 23rd, 2012, 12:30 PM
#3
Thread Starter
New Member
Re: How to sort dataset and save as xml
Originally Posted by MattP
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
-
Feb 23rd, 2012, 12:36 PM
#4
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
-
Feb 23rd, 2012, 12:39 PM
#5
Thread Starter
New Member
Re: How to sort dataset and save as xml
Originally Posted by Shaggy Hiker
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?
-
Feb 23rd, 2012, 12:47 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|