Export (0) Print
Expand All

Adding Sorting to a DataGrid Web Server Control

Visual Studio .NET 2003

The DataGrid Web server control provides a way for you to add sorting to the grid using these methods:

  • Default sorting    All columns in the grid can be sorted. The header for each column contains a LinkButton control (hyperlink) that users click to sort by that column.
  • Custom sorting    You define which columns can be sorted and what type of button in the column heading users click to sort.

In either case, the grid does not sort the rows. Instead, it notifies you of the sort request by raising an event. You then perform the sort in your own code, usually by sorting and then rebinding to the data source.

Specifying Default Sorting

To use default sorting, you set properties of the DataGrid control to enable sorting for all columns. You then create an event handler to perform the sort.

To specify default sorting

  1. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
  2. In the DataGrid Properties dialog box, click the General tab.
  3. Set the grid's AllowSorting property to true.

At run-time, the headers for all columns are automatically rendered as LinkButton controls. When the user clicks a column head, the name of the bound field for that column is passed as the sort key.

Specifying Custom Sorting

You can define custom sorting by including bound columns and template columns in the grid and writing code in the SortCommand event to sort the data.

To specify custom sorting

  1. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
  2. In the DataGrid Properties dialog box, click the General tab.
  3. In the Behavior section, select the Allow sorting box.
  4. Switch to the Columns tab.
  5. Add any combination of bound columns and template columns to the DataGrid control. For details, see Adding Bound Columns to a DataGrid Web Server Control and Adding Template Columns to a DataGrid Web Server Control.
  6. To enable sorting for a column, enter a value for the Sort expression for that column. This can be any string value, but is typically the name of the data field to which the column is bound.
    Note   Columns without a sort expression will not raise the SortCommand event.
  7. Add an event handler for the SortCommand event and continue as described in the section below, "Performing the Sort." For more information on event handlers, see Creating Event Handlers in Web Forms Pages.

If you include template columns, you can specify what type of button a user clicks in the column heading to raise the SortCommand event.

To specify a button type for sorting in a template column

  1. Select the template column to work with and enter a value for its Sort expression. This can be any string value, but is typically the name of the data field to which the column is bound.
  2. Edit the header template for that column. For details about editing templates, see Creating Web Server Control Templates.
  3. Add a Button, LinkButton, or ImageButton control to the template and set its properties to specify a caption or graphic.
  4. Set the Command property of the button to sort (case-sensitive).
  5. Add an event handler for the SortCommand event and continue as described in the next section, "Performing the Sort." For more information on event handlers, see Creating Event Handlers in Web Forms Pages.

Performing the Sort

When the user clicks the button or hyperlink in the header that has sorting enabled, the SortCommand event for the DataGrid is raised. In the event handler, you sort the data and rebind it to the DataGrid.

To respond to a sort request

  • Create an event handler for the grid's SortCommand event. In the method:
    1. Get the sort key (the value you set in the Sort expression) from the SortExpression property of the DataGridSortCommandEventArgs argument.
    2. Sort the data.
    3. Rebind the data from the data source.

      The following example shows how to sort by handling the SortCommand event. The method gets the sort key value and uses it to set the Sort property of a DataView object. The code then rebinds the grid to the DataView object, which automatically returns the data in sorted order.

      ' Visual Basic
      Private Sub DataGrid1_SortCommand(ByVal source As Object, _
      ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
      Handles DataGrid1.SortCommand
         DataView1.Sort = e.SortExpression
         DataGrid1.DataBind()
      End Sub
      
      // C#
      private void DataGrid1_SortCommand(object source, 
      System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
      {
         dataView1.Sort = e.SortExpression;
         DataGrid1.DataBind();
      }
      

See Also

Adding DataGrid Web Server Controls to a Web Forms Page | DataGrid Web Server Control | Introduction to the DataGrid Web Server Control | Paging Behavior in DataGrid Web Server Controls

Show:
© 2015 Microsoft