Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to pass sorted Data from a DataSet to a DataGrid

Posted on 2006-04-02
5
Medium Priority
?
248 Views
Last Modified: 2010-04-17
Below is the structure of an XML file i have. I have populated a DataSet with the Data from the XML file. What i want to do is sort the data from the DataSet and display the sorted data in a DataGrid. How do i do this?

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Customers>
    <Cus_fname>aaaaaaaa</Cus_fname>
    <Cus_lname>aaaaaaaaa</Cus_lname>
    <Tel>aaaaaaaa</Tel>
    <Add_Line1>aaaaaaaaa</Add_Line1>
    <Add_Line2>aaaaaaaa</Add_Line2>
    <Post_Code>aaaaaaa</Post_Code>
    <Day_Read>aaaaaaaa</Day_Read>
    <Night_Read>aaaaaa</Night_Read>
  </Customers>
  <Customers>
    <Cus_fname>bbb</Cus_fname>
    <Cus_lname>bbb</Cus_lname>
    <Tel>bbb</Tel>
    <Add_Line1 />
    <Add_Line2 />
    <Post_Code />
    <Day_Read>bbb</Day_Read>
    <Night_Read>bbb</Night_Read>
  </Customers>
</NewDataSet>


Below is the code i have written so far.

        Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click

        Dim ds As DataSet = New DataSet
        Dim Customers As DataTable
        Dim rowIndex As Integer
        Dim row As DataRow
        ds.ReadXml("Customers.xml")

        Customers = ds.Tables.Item(0)
        For rowIndex = 0 To Customers.Rows.Count - 1
            row = Customers.Rows.Item(rowIndex)

            If row("Cus_fname") = txtCustomerFirstName.Text  Then
               
                              'populate the dataGrid

            End If

        Next
    End Sub

What is the code for populating the sorted data to the DataGrid?
0
Comment
Question by:sergeiweerasuriya
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16356769
Here's how you can sort your data:

        ds.Tables(0).DefaultView.Sort = "columnName" 'the name of the field to use for sorting

and the following line will populate the DataGrid:

        MyDataGrid.DataSource = ds.Tables(0)

_______________

  Nayer Naguib
0
 

Author Comment

by:sergeiweerasuriya
ID: 16356798
what if i used two columns for sorting.
0
 

Author Comment

by:sergeiweerasuriya
ID: 16356806
Here's what i want to do. I want to put that particular row into the dataGrid. The following code puts all the rows in the DataSet to the Grid.

ds.ReadXml("Customers.xml")

        Customers = ds.Tables.Item(0)
        For rowIndex = 0 To Customers.Rows.Count - 1
            row = Customers.Rows.Item(rowIndex)
            If row("Cus_fname") = txtCustomerFirstName.Text Then
                txtCustomerFirstName.Text = row("Cus_fname")
                txtCustomerLastName.Text = row("Cus_lname")
                txtAddressLine1.Text = row("Add_Line1")
                txtAddressLine2.Text = row("Add_Line2")
                txtPostcode.Text = row("Post_Code")
                txtDayRead.Text = row("Day_Read")
                txtNightRead.Text = row("Night_Read")

                ds.Tables(0).DefaultView.Sort = "Cus_fname"
                DataGrid1.DataSource = ds.Tables(0)
            End If
0
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16356814
Use the expression:

        ds.Tables(0).DefaultView.Sort = "column1,column2,..."

_______________

  Nayer Naguib
0
 
LVL 14

Accepted Solution

by:
nayernaguib earned 1500 total points
ID: 16356837
Add the row(s) that you want to show in the DataGrid to a newly created table (foe example, ds.Tables(1)).
Next, set the data source of the DataGrid to this table.

_______________

  Nayer Naguib
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Starting up a Project
Screencast - Getting to Know the Pipeline

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question