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