GridView sorting

原创 2004年09月20日 20:24:00
 

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>

ASP.NET GridView分页+排序+自定义样式【常规功能比JS插件使用简单】

现在基本都是在用一些前台table框架,做一些分页加排序处理 脑子里面就记得用repeat 处理完table然后调用第三方tablejs插件做分页和排序。 感觉这样能够做到想要的效果。 但是用第...
  • qq873113580
  • qq873113580
  • 2016年05月16日 15:30
  • 1805

Asp.net中,点击GridView表头实现数据的排序

一、实现该功能的基本工作。 1、  先添加一个GridView,取名为gvData。 2、  设置该控件的属性: 操作步骤如下。 设置属性:    ...
  • hxj135812
  • hxj135812
  • 2014年03月10日 10:57
  • 7169

Asp.net中GridView使用详解

l         GridView无代码分页排序 l         GridView选中,编辑,取消,删除 l         GridView正反双向排序 l         GridView和...
  • zdhlwt2008
  • zdhlwt2008
  • 2016年05月13日 15:39
  • 8585

GridView Sorting

We're going to describe a GridView sorting method we used many times in recent projects.  Here are t...
  • L_Vincent
  • L_Vincent
  • 2011年08月29日 13:01
  • 526

sorting by using array

  • 2014年12月12日 14:33
  • 2KB
  • 下载

Cell sorting in a Petri dish controlled by computer vision

  • 2014年06月20日 21:35
  • 3.78MB
  • 下载

sorting排序算法

  • 2012年11月05日 09:20
  • 15KB
  • 下载

an o(nlogn) sorting network

  • 2011年01月09日 08:22
  • 540KB
  • 下载

js-sorting-algorithm

  • 2017年07月09日 23:04
  • 916KB
  • 下载

External sorting

  • 2008年06月01日 21:23
  • 94KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:GridView sorting
举报原因:
原因补充:

(最多只允许输入30个字)