asp.net如何实现datagrid的分页显示

willyu 2007-12-18 09:57:31
asp.net如何实现datagrid的分页显示
使用的是VS2003下VB.net来开发的
望指教,谢谢
...全文
225 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tdhao 2007-12-19
  • 打赏
  • 举报
回复
设置AllowPaging=true;
sunnystar365 2007-12-19
  • 打赏
  • 举报
回复
第一.使用DataGrid的自带的分页控件
第二.使用AspNetPager分页控件
第三.自己写分页 参考http://www.cnblogs.com/sunnystar365/archive/2005/09/28/245665.html
ASP_sem 2007-12-19
  • 打赏
  • 举报
回复
给你一个自定义分页的例子
.aspx
<%@ Page language="c#" Codebehind="DataGridCustomPage.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridCustomPage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridCustomPage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h2>DataGrid自定义分页的例子</h2>
<asp:datagrid id="dgCustomPage" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 48px"
runat="server" Width="376px" AutoGenerateColumns="False" CellPadding="4" BackColor="White"
BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC" Height="20px" AllowPaging="True"
AllowCustomPaging="True" PageSize="5">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="LastName" HeaderText="姓名"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="姓"></asp:BoundColumn>
<asp:BoundColumn DataField="City" HeaderText="城市"></asp:BoundColumn>
<asp:BoundColumn DataField="BirthDate" HeaderText="出生年月" DataFormatString="{0:D}"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>

.CS
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
/// <summary>
/// DataGridCustomPage 的摘要说明。
/// </summary>
public class DataGridCustomPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgCustomPage;
//定义全局变量用来保存每页的起始项索引
int startIndex=0;
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}
private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,BirthDate,City from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds,startIndex,dgCustomPage.PageSize,"CurDataTable");
//填充数据集
da.Fill(ds,"AllDataTable");
//设置DataGrid控件实际要显示的项数
dgCustomPage.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
dgCustomPage.DataSource = ds.Tables["CurDataTable"];
dgCustomPage.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgCustomPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgCustomPage_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgCustomPage_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
dgCustomPage.CurrentPageIndex = e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex = dgCustomPage.PageSize * dgCustomPage.CurrentPageIndex;
//重新绑定数据
DataGridDataBind();
}
}
}

duoduo123 2007-12-19
  • 打赏
  • 举报
回复
用自定义分页,然后按页码写一个绑定的函数去获取DT绑定
kbryant 2007-12-19
  • 打赏
  • 举报
回复
<asp:datagrid id="DataGrid1" runat="server" CellSpacing="0" CellPadding="2" PagerStyle-HorizontalAlign="Right" PageSize="3" AllowPaging="True" pagerstyle-nextpagetext="下一页" pagerstyle-prevpagetext="上一页" onpageindexchanged="changeage"></asp:datagrid>

然后在后台代码加入
sub changepage(s as object,e as datagridpagechangedeventargs)
datagrid1.currentpageindex=e.newpageindex
end sub

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧