c# gridview 排序及分页_c#应用
2008-02-23 05:42:13来源:互联网 阅读 ()
假如您在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">
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash