asp.net.ph

DataGrid.AllowSorting Property

System.Web.UI.WebControls Namespace   DataGrid Class


Sets or retrieves a value specifying whether sorting is enabled.

Syntax


Inline <asp:datagrid allowsorting = true | false ... >
Script DataGrid.AllowSorting [ = true | false ]

Property Value

This property accepts or returns only a boolean value: true if sorting is enabled; otherwise, false. Default value is false.

Remarks

Use the AllowSorting property to specify or determine whether sorting is enabled or disabled in a DataGrid control.

When sorting is enabled in a DataGrid using default bound columns, LinkButton controls are rendered in the header section of each column, enabling users to set the order of the data in the grid by the selected column.

When a LinkButton in the columns header is clicked, a SortCommand event occurs. It is up to you, though, to provide code to handle this event. The typical logic for the handler is to pass the field or expression to sort by, and then rebind the data to the DataGrid control.

Example

The following code snippets demonstrate how to specify and code a handler for the SortCommand event to enable sorting in a DataGrid control.

The below code shows how to set the AllowSorting property and designate the handler for the SortCommand event at design time.

<asp:datagrid id = "myGrid" runat = "server"
   ...
   allowsorting
   onSortCommand = "sortGrid" />

The below shows the typical code for the SortCommand handler.

<script language = "C#" runat = "server">
   string query = "SELECT CompanyName, ContactName, ... FROM Suppliers";

   void Page_Load ( Object src, EventArgs e ) {
      if ( !IsPostBack ) {
         DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
         myView.Sort = "companyname";
         myGrid.DataSource = myView;
         myGrid.DataBind ( );
      }
   }

   void sortGrid ( Object src, DataGridSortCommandEventArgs e ) {
      DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
      myView.Sort = e.SortExpression;
      myGrid.DataSource = myView;
      myGrid.DataBind ( );
   }
</script>
  C# VB

 Show me 

See Also

DataGrid Members   SortCommand Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph