DataGrid传统分页方式

此分页方式与传统ASP分页方式相仿.

DataGridPage.aspx

1@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="netCRM.DataGridPage" 
 1<html>
 2<head>
 3<title>DataGridPage</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body ms_positioning="GridLayout">
10<form id="Form1" method="post" runat="server">
11<asp:datagrid alternatingitemstyle-backcolor="#eeeeee" bordercolor="Black" borderwidth="1px" cellpadding="3" font-name="Verdana" font-names="Verdana" font-size="8pt" headerstyle-backcolor="#aaaadd" id="DataGrid1" pagerstyle-horizontalalign="Right" pagerstyle-mode="NumericPages" pagesize="5" runat="server" width="100%">
12<alternatingitemstyle backcolor="#EEEEEE"></alternatingitemstyle>
13<headerstyle backcolor="#AAAADD"></headerstyle>
14<pagerstyle horizontalalign="Right" mode="NumericPages"></pagerstyle>
15</asp:datagrid>
16</form>
17<table bgcolor="#aaaadd" border="0" cellpadding="1" cellspacing="0" width="100%">
18<tbody>
19<tr>
20<td>
21<table bgcolor="#fef8e2" border="0" cellpadding="4" cellspacing="0" width="100%">
22<tbody>
23<tr>
24<td align="center" class="M" nowrap=""><asp:literal id="Literal1" runat="server"></asp:literal></td>
25</tr>
26<tr>
27<td align="center" class="C" nowrap=""><asp:literal id="Literal2" runat="server"></asp:literal></td>
28</tr>
29</tbody>
30</table>
31</td>
32</tr>
33</tbody>
34</table>
35</body>
36</html>

DataGridPage.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace netCRM
{
///

1<summary>   
2/// DataGridPage 的摘要说明。   
3/// </summary>

public class DataGridPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;
protected System.Web.UI.WebControls.Literal Literal2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
string connstring = "Server=.;Database=NorthWind;User Id=sa;Password=;";
string sql="Select * from Orders";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql,conn);
sqlAdapter.Fill(ds,"users");

DataView dataview = new DataView();
dataview = ds.Tables[0].DefaultView;

DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();

string cPage;
int pageSize = 10;
int currentPage;
int pageCount;
int numResults = 0;

if (Request.QueryString["page"]==null)
{
cPage="1";
}
else
{
cPage=Request.QueryString["page"].ToString();
}
try
{
currentPage = Int32.Parse(cPage);
}
catch
{
currentPage = 1;
}

numResults = 0;
int start = (int)((currentPage - 1) * pageSize);
int to = (int)(currentPage * pageSize);
if (start <= 0) start = 0;

numResults = dataview.Count;
int a1=0;
pageCount = Math.DivRem(numResults,pageSize,out a1);
if (a1>0)
{
pageCount++;
}
if(currentPage>pageCount || currentPage<=0)
{
currentPage = 1;
}
if(currentPage==pageCount)
{
to = dataview.Count;
}

// Create one DataTable with one column.
DataTable myTable = new DataTable("myTable");
myTable = dataview.Table.Clone();

//DataColumn colItem1 = new DataColumn("name",Type.GetType("System.String"));
//DataColumn colItem2 = new DataColumn("types",Type.GetType("System.String"));
//DataColumn colItem3 = new DataColumn("vendor",Type.GetType("System.String"));
//myTable.Columns.Add(colItem1);
//myTable.Columns.Add(colItem2);
//myTable.Columns.Add(colItem3);

//add row
DataRow NewRow;
for(int i=start;i

 1<numresults;i++) <summary="" datagrid1.databind();="" datagrid1.datasource="resultDataview;" dataview="" dataview(mytable);="" for(int="" if(i<to)="" k="0;k&lt;dataview.Table.Columns.Count;k++)" mytable.acceptchanges();="" mytable.rows.add(newrow);="" newrow="myTable.NewRow();" newrow[k]="dataview.Table.Rows[i][k];" resultdataview="new" {="" }="">   
 2/// 生成页导航条。   
 3///    
 4string strNav = "";   
 5int endpage;   
 6if (currentPage&gt;1)   
 7{   
 8strNav += "<a href='?page="+ (currentPage-1).ToString() +"'>上一页</a> ";   
 9}   
10if (currentPage&gt;11)   
11{   
12strNav += "<a href="?page=1">1</a> ...";   
13}   
14if(pageCount&gt;currentPage+10)   
15{   
16endpage = currentPage+10;   
17}   
18else   
19{   
20endpage = pageCount;   
21}   
22for (int i=currentPage-10;i<endpage+1;i++) if(i="" {="">=1)   
23{   
24if (i==currentPage)   
25{   
26strNav +="<font color="#990000"><strong>"+ i.ToString() +"</strong></font> ";   
27}   
28else   
29{   
30strNav += "<a href='?page="+ i.ToString() +"'>"+ i.ToString() +"</a> ";   
31}   
32}   
33}   
34if((currentPage+10)<pagecount) +="" +"'="" pagecount.tostring()="" strnav="" {="">"+ pageCount.ToString() +"";   
35}   
36if(currentPage<pagecount) (currentpage+1).tostring()="" +="" +"'="" strnav="" {="">下一页 ";   
37} 
38
39Literal1.Text = strNav;   
40Literal2.Text = "共 "+ numResults.ToString() +" 条供应信息,当前显示第 "+   
41(start+1).ToString() +" - "+ to.ToString() +" 条,共 "+ pageCount.ToString() +" 页";   
42} 
43
44#region Web 窗体设计器生成的代码   
45override protected void OnInit(EventArgs e)   
46{   
47//   
48// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。   
49//   
50InitializeComponent();   
51base.OnInit(e);   
52}   
53  
54/// <summary>   
55/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
56/// 此方法的内容。   
57/// </summary>   
58private void InitializeComponent()   
59{   
60this.Load += new System.EventHandler(this.Page_Load); 
61
62}   
63#endregion   
64}   
65}</pagecount)></pagecount)></endpage+1;i++)></numresults;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus