How to Sort Columns in DataGrid

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.

Instructions

  1. Enable Default Sorting

    • 1

      Right click on your DataGrid in the design view.

    • 2

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

    • 3

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

    • 4

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

    • 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

      Click the \"OK\" button to save your changes.

    Custom Bi-Directional Sorting

    • 1

      Perform all the steps for enabling default sorting.

    • 2

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

    • 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

      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\"

      GO

         else
      Session[\"SortOrder\"] = \"ASC\"

      GO

       }
      else
      {
      Session[\"SortBy\"] = e.SortExpression.ToString()

      GO

         Session[\"SortOrder\"] = \"ASC\"

      GO

       }

      // CODE FOR DATA BINDING GOES HERE

      }

    • 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\"])

      GO

       myDataView.Sort = Session[\"SortBy\"] & \" \" & Session[\"SortOrder\"]

      GO

       myGrid.Datasource = myDataView

      GO

       myGrid.DataBind()

      GO
      }

Tips & 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.
Related Searches

References

  • Photo Credit table and pencil image by Daraban Oana Gabriela from Fotolia.com

You May Also Like

Related Ads

View Blog Post

Enter the Geek Vs Geek Giveaway and Win a Roku!