Results 1 to 4 of 4

Thread: Sorting Data in DataGrid

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52

    Sorting Data in DataGrid

    Hi;
    I am able to display the data from DB2 database in a DataGrid on a WebForm. But, I just can't figure out how to sort the data on the form. I did look in the MSDN library but it didn't help me much.
    I did go to the "Property builder" of the datagrid and turned on the "Sorting Behavior" and I have put the "Sorting Expression" in the property builder also. But I think we need to put some code also to be able to sort the data during the run time.
    So, could someone please let me know, what code I need to put to be able to sort the data?

    thanks in advance

  2. #2
    Member
    Join Date
    Sep 2002
    Posts
    32
    when you click on the column header you fire of the Sort_command property. the e.SortExpression tells you which column has been clicked. Re populate the grid using this ie.

    "select * from Customers Order By " & e.sortExpression

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52
    I kind of tried that........ but the code is as follows

    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand

    DataView1.Sort = e.SortExpression
    DataGrid1.DataSource = DataView1
    DataGrid1.DataBind()
    End Sub

    now; when click the column header while running the application to sort the data the page refreshes but nothing changes. So do you have any idea what would be the problem.

    thanks
    Jacob

  4. #4
    Member
    Join Date
    Sep 2002
    Posts
    32
    I'm not sure about your code, but this is how i do it. I initially populate the grid , then when the header is clicked run this:

    Dim myDataSet As New DataSet()
    Dim mySqlDataAdapter As SqlDataAdapter
    mySqlDataAdapter = New SqlDataAdapter( _
    "SELECT * From tbl_employee ORDER BY " & e.SortExpression, _
    "Data Source=NTTEST;Initial Catalog=Payroll;User id=Sa;Pwd=;")
    mySqlDataAdapter.Fill(myDataSet, "Person")
    GrdRepairs.DataSource = myDataSet.Tables("Person")
    GrdRepairs.DataBind()

    I'm sure there maybe a better way of doing this but it works for me.

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