How to Sort Columns in DataGrid

Written by susan hare
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Sort Columns in DataGrid
DataGrids can present information in an easily understandable format. (table and pencil image by Daraban Oana Gabriela from Fotolia.com)

DataGrids are .NET controls that allow users to display data in a table-like fashion, but with much more functionality and options. DataGrids can support tasks like inserting, editing and deleting data. They can also be paged, which means that you can show smaller amounts of data on your screen at a time and users can flip through them. Sorting in descending order is another feature of DataGrids, however, without custom coding, they cannot be sorted in ascending order. A sortable DataGrid has link buttons for column headers and will update the grid when the user clicks on them.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Right click on your DataGrid in the design view.

  2. 2

    Select "Property Builder" from the pop-up menu.

  3. 3

    Mark the check box for "Allow sorting" under the "Behavior" section of the "General" tab.

  4. 4

    Click on the "Columns" tab in the menu at the left.

  5. 5

    Make sure that each column is sortable by clicking on its name in the "Selected Columns" list and then choosing a value in the "Sort Expression" drop-down list.

  6. 6

    Click the "OK" button to save your changes.

  1. 1

    Perform all the steps for enabling default sorting.

  2. 2

    Press the F7 key to switch to "Code Editor" view.

  3. 3

    Select your DataGrid from the drop-down list of controls at the top left of the window, and then select the "Sort Command" event from the drop-down list of events at the top right. This will create an event handler for you to override the default sorting.

  4. 4

    Add code to your sort event handler to determine the appropriate sort order and the column to sort by. Copy the code below into your code file.

    ' VB.NET Example Sub myGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs) Handles myGrid.SortCommand If e.SortExpression.ToString() = Session("SortBy") Then If Session("SortOrder") = "ASC" Then Session("SortOrder") = "DESC" Else Session("SortOrder") = "ASC" End If Else Session("SortBy") = e.SortExpression.ToString() Session("SortOrder") = "ASC" End If

     ' CODE FOR DATA BINDING GOES HERE
    

    End Sub

    // C# Example void myGrid_Sort(Object sender, DataGridSortCommandEventArgs e) { if (e.SortExpression.ToString() == Session["SortBy"]) { if (Session["SortOrder"] == "ASC") Session["SortOrder"] = "DESC"; else Session["SortOrder"] = "ASC"; } else { Session["SortBy"] = e.SortExpression.ToString(); Session["SortOrder"] = "ASC"; }

     // CODE FOR DATA BINDING GOES HERE
    

    }

  5. 5

    Add code to your sort event handler to rebind the DataGrid with the proper sorting. Combine the code below with the event handler already in your code file.

    ' VB.NET Example Sub myGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs) Handles myGrid.SortCommand ' CODE FOR SORT ORDER GOES HERE

     Dim myDataView As New DataView(myDataSource.Tables("myTable"))
     myDataView.Sort = Session("SortBy") & " " & Session("SortOrder") 
     myGrid.Datasource = myDataView
     myGrid.DataBind()
    

    End Sub

    // C# Example void myGrid_Sort(Object sender, DataGridSortCommandEventArgs e) { // CODE FOR SORT ORDER GOES HERE

     DataView myDataView = new DataView(myDataSource.Tables["myTable"]);
     myDataView.Sort = Session["SortBy"] & " " & Session["SortOrder"];
     myGrid.Datasource = myDataView;
     myGrid.DataBind();
    

    }

Tips and warnings

  • An alternative to editing your DataGrid in design view to achieve default sorting is to edit it in source view and add the "Allow Sorting" property to your DataGrid tag definition and set it to "True."
  • The DataView object can also be used to filter the contents of your DataGrid with the RowFilter property.
  • The DataGrid has been replaced by the GridView in versions of .NET after 2.0. The GridView control is very similar but encompasses more functionality.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.