CSDN博客

img waterboy

GridView sorting

发表于2004/9/20 20:24:00  1891人阅读

分类: WEBFORM

 

Introduction

 

As you may know the GridView have built in support of sorting. In the current version of ASP.Net 1.x, we don’t have the GridView control. Instead you have the DataGrid control. To handle the sorting of the DataGrid control you have to build your own sorting logic. This is not required with the new GridView control.

 

To enable the built in sort functionality of the GridView control, you set the AllowSorting property of the GridView to true. When the AllowSorting property is set to true the GrdiView will add a LinkButton for each column header and set the SortExpression to the filed bind to each column. For example, if the one column is bind to a filed, e.g. CustomerName the SortExpression property will be CustomerName. Setting the grid's AllowSorting property allows you to sort columns by default. You can disable sorting for individual fields (BoundColumn or TemplateColumns fields) by setting the SortExpression property of the individual column to an empty string.

 

The sorting is handled by the data source control associated to the GridView control. Not all data source controls can sort data. The XmlDataSource can't sort any data. If the data source control associated to a GridView control supports sorting, the DataView object exposed by the data source control will set its CanSort property to true. For the GridView control to be able to display sorted data, the data source control to which it is bound must meet certain criteria:

 

The SqlDataSource control can sort if its DataSourceMode property is set to DataSet, or its SortParameterName is set to the name of an input parameter that a Stored Procedure will use to sort data.

 

The ObjectDataSource control can sort if its SortParameterName property is set or if the Select method returns a DataSet.

 

The XmlDataSource control does not support sorting.

 

The DataSetDataSource control supports sorting.

 

The AccessDataSource control can sort.

 

How the sorting works

 

When you press the LinkButton for the column you want to be sorted, the Sorting event will be raised. The GridView passes the sort expression to the data source control and the data source control executes its SelectCommand with the sort expression as an argument. When this is done, the GridView will rebind it self.

 

If the SqlDataSource control is used and the DataSourceMode property is set to DataSet, the sort expression will be set to the DataView’s Sort property of the first table in the returned DataSet. If the DataSourceMode is set to DataReader, the SortExpression will not work; if you use it an exception will be thrown. To sort a DataReder the SortParameterName must be used. If you specify a value to the SortParameterName, you must use a stored procedure. The stored procedure must have an input parameter with the same name as the value specified in the SortParameterName property. If you set the SortParameterName to “sort” you must have a “@sort” input parameter added to the stored procedure. The SqlDataSource will pass the sort expression to the stored procedure’s parameter.

 

If an ObjectDataSource is used and the SelectCommand method returns a DataSet the sort expression will be set to the DataView Sort property of the first table in the DataSet. If the SortParameterName is specified for the ObjectDataSource, the sort expression will be passed to the argument of the SelectCommand method with the same name as specified in the SortParameterName.

 

The DataSetDataSource will set the sort expression to the DataView Sort property for the first table in the DataSet.

 

The AccessDataSource inherits the SqlDataSource control, so it will use the same sorting functionality that the SqlDataSource has.

 

By default the sorting of a column in the GridView control will only sort on one column. To sort on several columns you can use the Sorting event of the GridView to change the sort expression. The following example uses a SqlDataSource control with its DataSourceMode is set to DataSet (This is the default mode of the SqlDataSource control), when one of the LinkButtons in the header of the GridView is pressed, the Sorting event will be executed. If the CompanyName column LinkButton is pressed, the sort expression will be changed and an ascending sort on both CompanyName and the ContactName columns will be made:

 

<%@ Page Language="C#" ClassName="Default_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

 

<script runat=server>

    void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    {

        if (e.SortExpression == "CompanyName")

        {

            e.SortExpression = e.SortExpression + ", ContactName";

        }

    }   

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"

            AllowSorting="True" AllowPaging="True" OnSorting="GridView1_Sorting" Font-Bold="True">

            <Columns>

                <asp:BoundField HeaderText="CompanyName" DataField="CompanyName" SortExpression="CompanyName"></asp:BoundField>

                <asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName"></asp:BoundField>

                <asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle" SortExpression="ContactTitle"></asp:BoundField>

            </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"

            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

        </asp:SqlDataSource>

   

    </div>

    </form>

</body>

</html>

0 0

相关博文

我的热门文章

img
取 消
img