如果你在GridView控件上设置 AllowPaging=”true” or AllowSorting=”true” 而没有使用使用数据源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),运行则会出现下列错误: 当你在GridView控件上单击下一页时: The GridView GridViewID fired event PageIndexChanging which wasnt handled. 当你点击排序时,则回出现: The GridView GridViewID fired event Sorting which wasnt handled. 如果你没有设置GridView的DataSourceID 的属性,你必须添加一个操作才可以排序及分页。。 <%@ 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 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>
点这里查看下面的例子
http://www.cnblogs.com/cygoodyu/archive/2006/11/29/575825.html
c# gridview 排序及分页_c#应用
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c# gridview 排序及分页_c#应用
相关推荐
-      利用c#远程存取access数据库_c#应用
-      c# 3.0新特性系列:隐含类型var_c#教程
-      c#动态生成树型结构的web程序设计_c#应用
-      论c#变得越来越臃肿是不可避免的_c#应用
-      用c#监控并显示cpu状态信息_c#应用
-      c#中实现vb中的createobject方法_c#应用
-      photoshop给花瓶打造彩绘效果_photoshop教程
-      使用c#创建sql server的存储过程_c#应用