GridView sorting

 

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值