C# GridView 排序及分页

转载 2007年10月15日 19:58:00

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

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

 

<script runat="server">   

    private void PopulatePublishersGridView()

    {

        string connectionString = AccessConnectionString();

        OleDbConnection accessConnection = new OleDbConnection(connectionString);

 

        string sqlQuery = "SELECT [PubID], [Name], [Company Name], [Address], [City], [State], [Zip], [Telephone], [Fax], [Comments] FROM Publishers ORDER BY [Name] ASC;";

 

        OleDbCommand accessCommand = new OleDbCommand(sqlQuery, accessConnection);

 

        OleDbDataAdapter publishersDataAdapter = new OleDbDataAdapter(accessCommand);

        DataTable publishersDataTable = new DataTable("Publishers");

        publishersDataAdapter.Fill(publishersDataTable);

 

        int dataTableRowCount = publishersDataTable.Rows.Count;

 

        if (dataTableRowCount > 0)

        {

            gridViewPublishers.DataSource = publishersDataTable;

            gridViewPublishers.DataBind();

        }

    }

 

    private string AccessConnectionString()

    {

        string accessDatabasePath = Server.MapPath("~/App_Data/biblio.mdb");

        return String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", accessDatabasePath);

    }

 

    private string GridViewSortDirection

    {

        get { return ViewState["SortDirection"] as string ?? "ASC"; }

        set { ViewState["SortDirection"] = value; }

    }

 

    private string GridViewSortExpression

    {

        get { return ViewState["SortExpression"] as string ?? string.Empty; }

        set { ViewState["SortExpression"] = value; }

    }

 

    private string GetSortDirection()

    {

        switch (GridViewSortDirection)

        {

            case "ASC":

                GridViewSortDirection = "DESC";

                break;

 

            case "DESC":

                GridViewSortDirection = "ASC";

                break;

        }

 

        return GridViewSortDirection;

    }

 

    protected void gridViewPublishers_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource as DataTable, true);

        gridViewPublishers.PageIndex = e.NewPageIndex;

        gridViewPublishers.DataBind();

    }

 

    protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)

    {

        if (dataTable != null)

        {

            DataView dataView = new DataView(dataTable);

            if (GridViewSortExpression != string.Empty)

            {

                if (isPageIndexChanging)

                {

                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);

                }

                else

                {

                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());

                }

            }

            return dataView;

        }

        else

        {

            return new DataView();

        }

    }

 

    protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)

    {

        GridViewSortExpression = e.SortExpression;

        int pageIndex = gridViewPublishers.PageIndex;

        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource as DataTable, false);

        gridViewPublishers.DataBind();

        gridViewPublishers.PageIndex = pageIndex;

    }

 

    protected void Page_Load(object sender, EventArgs e)

    {

        PopulatePublishersGridView();

    }

 

</script>


 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head id="Head1" runat="server">

    <title>GridView Sorting/Paging without a DataSourceControl DataSource</title>

</head>

<body>

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

        <div>

            <asp:GridView ID="gridViewPublishers" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"

                EmptyDataText="No records found" PagerSettings-Mode="NumericFirstLast" PageSize="25"

                OnPageIndexChanging="gridViewPublishers_PageIndexChanging" OnSorting="gridViewPublishers_Sorting"

                runat="server">

                <AlternatingRowStyle BackColor="LightGray" />

                <HeaderStyle BackColor="Gray" Font-Bold="true" Font-Names="Verdana" Font-Size="Small" />

                <PagerStyle BackColor="DarkGray" Font-Names="Verdana" Font-Size="Small" />

                <RowStyle Font-Names="Verdana" Font-Size="Small" />

                <Columns>

                    <asp:BoundField DataField="PubID" HeaderText="Publisher ID" SortExpression="PubID" />

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

                    <asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="Company Name" />

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

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

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

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

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

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

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

                </Columns>

            </asp:GridView>

        </div>

    </form>

</body>

</html>

GridView 实现分页和排序

设计界面程序:http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/1999/xhtml" >    无...
  • wzhh598
  • wzhh598
  • 2006-04-28 10:52:00
  • 3165

动态绑定GridView并自定义分页

后台代码 using System; using System.Data; using System.Configuration; using System.Web; using Syste...
  • drr789
  • drr789
  • 2012-09-06 08:52:12
  • 4191

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

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

GridView无代码分页排序

1.GridView无代码分页排序: 效果图: 1.AllowSorting设为True,aspx代码中是AllowSorting="True"; 2.默认1页10条,如果要修改每页条数,修改Pag...
  • gsky
  • gsky
  • 2008-03-25 22:43:00
  • 822

C#万能分页代码,GridView分页相当方便

  • 2010年01月07日 10:31
  • 2KB
  • 下载

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

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

c# gridview 分页

http://www.aspsnippets.com/Articles/Implement-Paging-DataGridView-in-Windows-Forms-WinForms-Applicat...
  • u014683488
  • u014683488
  • 2016-08-03 22:26:43
  • 402

C#中GridView的排序用法

  此方法摘录于:http://topic.csdn.net/u/20071109/20/C598BB6E-4CA8-4CD0-8204-E42A315FD5D0.html1、利用DataView来进...
  • yc_8301
  • yc_8301
  • 2008-03-29 13:08:00
  • 4565

GridView控件实现分页功能

初识ASP.NET中的控件,还是折腾了一小会儿,就把我折腾的结果给大家看看: 表格显示数据时网站开发中的一个常见的功能,ASP.NET提供了很多工具在网页中显示表格,GridView控件就是最常见的...
  • wangboxian
  • wangboxian
  • 2012-04-14 10:37:36
  • 24197

Gridview分页代码C#

using System; using System.Data; using System.Configuration; using System.Collections; using System....
  • allen3010
  • allen3010
  • 2010-12-08 11:01:00
  • 3627
收藏助手
不良信息举报
您举报文章:C# GridView 排序及分页
举报原因:
原因补充:

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