急求(日落前给分!):如何让DataGrid分页显示,按BUTTON时显示下页?我看MSDN知此控件的服务器版有这个功能:) (windows application) 分页是这样的,你先确定datagrid是固定显示多少行,然后读取所有数据,求出要显示多少页,你读取数据时就限制读取多少行数据出来, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 分页是这样的,你先确定datagrid是固定显示多少行,然后读取所有数据,求出要显示多少页,你读取数据时就限制读取多少行数据出来, (本程序在.Net Framework Beta 2下测试通过) <% @ Page Language="C#" %><% @ Import Namespace="System.Data" %><% @ Import Namespace="System.Data.OleDb" %><Script Language="C#" Runat="Server">/*Create By 飞刀http://www.aspcn.com2001-7-25 01:44Support .Net Framework Beta 2*/OleDbConnection MyConn;int PageSize,RecordCount,PageCount,CurrentPage;public void Page_Load(Object src,EventArgs e){//设定PageSizePageSize = 10;//连接语句string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";MyConn = new OleDbConnection(MyConnString);MyConn.Open();//第一次请求执行if(!Page.IsPostBack){ListBind();CurrentPage = 0;ViewState["PageIndex"] = 0;//计算总共有多少记录RecordCount = CalculateRecord();lblRecordCount.Text = RecordCount.ToString();//计算总共有多少页PageCount = RecordCount/PageSize;lblPageCount.Text = PageCount.ToString();ViewState["PageCount"] = PageCount;}}//计算总共有多少条记录public int CalculateRecord(){int intCount;string strCount = "select count(*) as co from Score";OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);OleDbDataReader dr = MyComm.ExecuteReader();if(dr.Read()){intCount = Int32.Parse(dr["co"].ToString());}else{intCount = 0;}dr.Close();return intCount;}ICollection CreateSource(){int StartIndex;//设定导入的起终地址StartIndex = CurrentPage*PageSize;string strSel = "select * from Score";DataSet ds = new DataSet();OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);MyAdapter.Fill(ds,StartIndex,PageSize,"Score");return ds.Tables["Score"].DefaultView;}public void ListBind(){score.DataSource = CreateSource();score.DataBind();lbnNextPage.Enabled = true;lbnPrevPage.Enabled = true;if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;if(CurrentPage==0) lbnPrevPage.Enabled = false;lblCurrentPage.Text = (CurrentPage+1).ToString();}public void Page_OnClick(Object sender,CommandEventArgs e){CurrentPage = (int)ViewState["PageIndex"];PageCount = (int)ViewState["PageCount"];string cmd = e.CommandName;//判断cmd,以判定翻页方向switch(cmd){case "next":if(CurrentPage<(PageCount-1)) CurrentPage++;break;case "prev":if(CurrentPage>0) CurrentPage--;break;}ViewState["PageIndex"] = CurrentPage;ListBind();}</script><html><head><title></title></head><body><form runat="server">共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList id="score" runat="server"HeaderStyle-BackColor="#aaaadd"AlternatingItemStyle-BackColor="Gainsboro"EditItemStyle-BackColor="yellow"><ItemTemplate>姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %><asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" /></ItemTemplate></asp:DataList><asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" /><asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /></form></body></html> to Happyboy_zjy(阳光男孩):windows application to love722915(xiaojun):能不不说得再细点,谢! windows Form的DataGrid不提供分页功能,而且在windows应用程序中分页的意义也并不大,你可以用其他更炫的功能实现你的想法,如进行递增过滤等。至于asp.net得分也没有上面仁兄说的复杂,只需要设置几个属性,写一行代码就行了。 asp.net中的分页的确如meditate所述一样,只用设置一些属性,自动进行分页,并且自动出现前、后翻动按钮。只是不知mediate所说的在windows form中的“更炫”的方法是什么? 感谢您使用微软产品。在.NET Framework中,您可以通过DataAdapter的如下Fill方法,来填充一页的记录:public int Fill( DataSet dataSet, int startRecord, int maxRecords, string srcTable);下面提供一段示例代码,供您参考: public class Form1 : System.Windows.Forms.Form { int currentIndex = 0; int pageSize =10; int totalRecords = 0; DataSet myDs; SqlDataAdapter myDa; …… private void Form1_Load(object sender, System.EventArgs e) { string connStr = "Server=SHA-RICKIE-01;Database=northwind;uid=user;pwd=user"; SqlConnection myConn = new SqlConnection(connStr); myConn.Open(); string sqlStr = "Select CustomerID, CompanyName FROM Customers"; myDs = new DataSet(); myDa = new SqlDataAdapter(sqlStr,myConn); myDa.Fill(myDs,currentIndex,pageSize,"Customers"); // Determine total pages. SqlCommand totCMD = new SqlCommand("SELECT Count(*) FROM Customers", myConn); totalRecords = (int)totCMD.ExecuteScalar(); myConn.Close(); dataGrid1.DataSource = myDs.Tables["Customers"].DefaultView; }//Next Button private void btnNext_Click(object sender, System.EventArgs e) { currentIndex = currentIndex + pageSize; if(currentIndex >= totalRecords) currentIndex = currentIndex - pageSize; myDs.Tables["Customers"].Clear(); myDa.Fill(myDs,currentIndex,pageSize,"Customers"); }//Previous Button private void btnPrevious_Click(object sender, System.EventArgs e) { currentIndex = currentIndex - pageSize; if(currentIndex < 0) currentIndex = 0; myDs.Tables["Customers"].Clear(); myDa.Fill(myDs,currentIndex,pageSize,"Customers"); } }Hope this is helpful! — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。 vs2005设置焦点是什么回事? 怎样将SESSION 保存在SQL数据库中?/?/ 小弟求vc#教程下载请大哥给个方便 onPrintPage()问题 为什么在窗体类中 带数据库 访问 的 方法 不能连续执行? 这个问题该怎么解决??急问 复制XML结点 c# 调用线程处理耗时操作返回结果 怎么实现 怎样引用命名控件,在线等待 今天搜索了各大人才网站,发现很少招聘C#程序员,是不是意味着..... C#的初级问题: 有人能提供一个使用.Net(C#)写的COM组件, 同时在VB6.0(or CV)中调用的例子嘛?
你先确定datagrid是固定显示多少行,
然后读取所有数据,求出要显示多少页,
你读取数据时就限制读取多少行数据出来,
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}ICollection CreateSource()
{int StartIndex;//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet();OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();}public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}ViewState["PageIndex"] = CurrentPage;ListBind();}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /></form>
</body>
</html>
windows application
能不不说得再细点,谢!
只是不知mediate所说的在windows form中的“更炫”的方法是什么?
public int Fill(
DataSet dataSet,
int startRecord,
int maxRecords,
string srcTable
);
下面提供一段示例代码,供您参考:
public class Form1 : System.Windows.Forms.Form
{
int currentIndex = 0;
int pageSize =10;
int totalRecords = 0;
DataSet myDs;
SqlDataAdapter myDa;
……
private void Form1_Load(object sender, System.EventArgs e)
{
string connStr = "Server=SHA-RICKIE-01;Database=northwind;uid=user;pwd=user";
SqlConnection myConn = new SqlConnection(connStr);
myConn.Open();
string sqlStr = "Select CustomerID, CompanyName FROM Customers";
myDs = new DataSet();
myDa = new SqlDataAdapter(sqlStr,myConn);
myDa.Fill(myDs,currentIndex,pageSize,"Customers");
// Determine total pages.
SqlCommand totCMD = new SqlCommand("SELECT Count(*) FROM Customers", myConn);
totalRecords = (int)totCMD.ExecuteScalar(); myConn.Close();
dataGrid1.DataSource = myDs.Tables["Customers"].DefaultView;
}
//Next Button
private void btnNext_Click(object sender, System.EventArgs e)
{
currentIndex = currentIndex + pageSize;
if(currentIndex >= totalRecords) currentIndex = currentIndex - pageSize;
myDs.Tables["Customers"].Clear();
myDa.Fill(myDs,currentIndex,pageSize,"Customers");
}
//Previous Button
private void btnPrevious_Click(object sender, System.EventArgs e)
{
currentIndex = currentIndex - pageSize;
if(currentIndex < 0) currentIndex = 0;
myDs.Tables["Customers"].Clear();
myDa.Fill(myDs,currentIndex,pageSize,"Customers");
}
}
Hope this is helpful!
— 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。