Results 1 to 11 of 11

Thread: Sorting Datasets: Can it be done?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Adelaide - Australia
    Posts
    150

    Sorting Datasets: Can it be done?

    I have a dataset which reads in an XML file containing an address book. I want to be able to sort this address book into alphabetical order. I have currently been taking the data from the dataset and inserting then sorting it in a collection. But that is very in-efficient.

    Is it possible to sort the dataset into alphabetical order from fields i define?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Just load the Dataset into a Dataview and sort it at the time. I don't have VS.NET on this computer but there should be an example either in the help or in the forums.

  3. #3
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    I tried doing this but everytime I click to sort the datagrid comes up empty!

    Code:
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                SqlDataAdapter1.SelectCommand.Parameters("@strDepartment").Value = "web"
                SqlDataAdapter1.Fill(Ds1)
                DataGrid1.DataBind()
                Cache.Insert("ds", Ds1, Nothing, DateTime.Now.AddMinutes(5), Caching.Cache.NoSlidingExpiration)
            End If
        End Sub
    
    
        Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
            Dim newdv As DataView = New DataView(Cache("ds"))
            Dim SortExpression As String = e.SortExpression.ToString()
            newdv.Sort = SortExpression & " asc"
            DataGrid1.DataSource = newdv
            DataGrid1.DataBind()
        End Sub
    End Class

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Try sorting the DataTables contained within the dataset (using your DataView) before binding (ie - do it in your load event).

  5. #5
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I think that the DataGrid has a property to allow sorting too, so you can also set that to True to allow your users to sort by any of the columns in your datagrid.

  6. #6
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by VBCrazyCoder
    I think that the DataGrid has a property to allow sorting too, so you can also set that to True to allow your users to sort by any of the columns in your datagrid.

    Well ya! But you have to code it yourself!

  7. #7
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Not if you have a custom Datagrid control

  8. #8
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by VBCrazyCoder
    Not if you have a custom Datagrid control
    WHAT?!

  9. #9
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You have to implement your own sorting routine for the Web DataGrid control. The Windows Form DataGrid sorting is free. Just click on the column header you want to sort. You can also sort on a specific column before you bind to the datagrid.

    Code:
    DataView dv = new DataView(ds.Tables["myTable"]);
    dv.Sort = "columnName DESC";
    dataGrid.DataSource = dv;

  10. #10
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by DevGrp
    You have to implement your own sorting routine for the Web DataGrid control. The Windows Form DataGrid sorting is free. Just click on the column header you want to sort. You can also sort on a specific column before you bind to the datagrid.

    Code:
    DataView dv = new DataView(ds.Tables["myTable"]);
    dv.Sort = "columnName DESC";
    dataGrid.DataSource = dv;

    But then HOW do you figure out what Sort Direction was previously selected? And also how do you resolve the issue if the user switches columns? The first click doesn't sort when they change columns.

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    What are you referring to? The Web DataGrid control or the Winforms DataGrid control?

Posting Permissions

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



Click Here to Expand Forum to Full Width