當前位置:編程學習大全網 - 編程語言 - 跪求分頁代碼 vs2005 c#分頁

跪求分頁代碼 vs2005 c#分頁

是.net編程吧、這個是winforms

我的能力只有 上壹頁 下壹頁這個程度了。

要是asp.net就好辦多了。

看代碼:

(包括窗體設計代碼,妳只需要測試壹下是不是妳想要的就好)

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace DataPage

{

/// <summary>

/// Form1 的摘要說明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.DataGrid dataGrid1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.ComboBox cboPageRows;

private System.Windows.Forms.GroupBox groupBox1;

private SqlConnection objCn;

private SqlCommand objCmd;

private DataSet objDs;

private SqlDataAdapter objAdapter;

private SqlDataReader objReader;

private int pageCount;//存放頁數

private int pageRows=5;//存放每頁可以顯示的數據行數

private int nowPage=1;//存放當前頁碼

private int start;

private int count;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Button btnFirst;

private System.Windows.Forms.Button btnBefore;

private System.Windows.Forms.Button btnNext;

private System.Windows.Forms.Button btnLast;//該數據源總***有多少條記錄

/// <summary>

/// 必需的設計器變量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗體設計器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼

//

}

/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗體設計器生成的代碼

/// <summary>

/// 設計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void InitializeComponent()

{

this.dataGrid1 = new System.Windows.Forms.DataGrid();

this.label1 = new System.Windows.Forms.Label();

this.cboPageRows = new System.Windows.Forms.ComboBox();

this.groupBox1 = new System.Windows.Forms.GroupBox();

this.btnLast = new System.Windows.Forms.Button();

this.btnNext = new System.Windows.Forms.Button();

this.btnBefore = new System.Windows.Forms.Button();

this.btnFirst = new System.Windows.Forms.Button();

this.label2 = new System.Windows.Forms.Label();

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

this.groupBox1.SuspendLayout();

this.SuspendLayout();

//

// dataGrid1

//

this.dataGrid1.AlternatingBackColor = System.Drawing.Color.GhostWhite;

this.dataGrid1.BackColor = System.Drawing.Color.GhostWhite;

this.dataGrid1.BackgroundColor = System.Drawing.Color.Lavender;

this.dataGrid1.CaptionBackColor = System.Drawing.Color.RoyalBlue;

this.dataGrid1.CaptionForeColor = System.Drawing.Color.White;

this.dataGrid1.DataMember = "";

this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;

this.dataGrid1.FlatMode = true;

this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);

this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;

this.dataGrid1.GridLineColor = System.Drawing.Color.RoyalBlue;

this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;

this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);

this.dataGrid1.HeaderForeColor = System.Drawing.Color.Lavender;

this.dataGrid1.LinkColor = System.Drawing.Color.Teal;

this.dataGrid1.Location = new System.Drawing.Point(0, 0);

this.dataGrid1.Name = "dataGrid1";

this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Lavender;

this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;

this.dataGrid1.SelectionBackColor = System.Drawing.Color.Teal;

this.dataGrid1.SelectionForeColor = System.Drawing.Color.PaleGreen;

this.dataGrid1.Size = new System.Drawing.Size(640, 280);

this.dataGrid1.TabIndex = 0;

//

// label1

//

this.label1.Location = new System.Drawing.Point(8, 304);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(120, 23);

this.label1.TabIndex = 1;

this.label1.Text = "設置每頁顯示的行數";

//

// cboPageRows

//

this.cboPageRows.Location = new System.Drawing.Point(16, 336);

this.cboPageRows.Name = "cboPageRows";

this.cboPageRows.Size = new System.Drawing.Size(88, 20);

this.cboPageRows.TabIndex = 2;

this.cboPageRows.Text = "--請選擇--";

this.cboPageRows.SelectedIndexChanged += new System.EventHandler(this.cboPageRows_SelectedIndexChanged);

//

// groupBox1

//

this.groupBox1.Controls.Add(this.btnLast);

this.groupBox1.Controls.Add(this.btnNext);

this.groupBox1.Controls.Add(this.btnBefore);

this.groupBox1.Controls.Add(this.btnFirst);

this.groupBox1.Location = new System.Drawing.Point(144, 296);

this.groupBox1.Name = "groupBox1";

this.groupBox1.Size = new System.Drawing.Size(448, 72);

this.groupBox1.TabIndex = 3;

this.groupBox1.TabStop = false;

this.groupBox1.Text = "控制按紐";

//

// btnLast

//

this.btnLast.Location = new System.Drawing.Point(352, 32);

this.btnLast.Name = "btnLast";

this.btnLast.TabIndex = 3;

this.btnLast.Text = "最後壹頁";

this.btnLast.Click += new System.EventHandler(this.btnLast_Click);

//

// btnNext

//

this.btnNext.Location = new System.Drawing.Point(240, 32);

this.btnNext.Name = "btnNext";

this.btnNext.TabIndex = 2;

this.btnNext.Text = "下壹頁";

this.btnNext.Click += new System.EventHandler(this.btnNext_Click);

//

// btnBefore

//

this.btnBefore.Location = new System.Drawing.Point(136, 32);

this.btnBefore.Name = "btnBefore";

this.btnBefore.TabIndex = 1;

this.btnBefore.Text = "上壹頁";

this.btnBefore.Click += new System.EventHandler(this.btnBefore_Click);

//

// btnFirst

//

this.btnFirst.Location = new System.Drawing.Point(32, 32);

this.btnFirst.Name = "btnFirst";

this.btnFirst.TabIndex = 0;

this.btnFirst.Text = "第壹頁";

this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);

//

// label2

//

this.label2.Location = new System.Drawing.Point(136, 392);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(288, 40);

this.label2.TabIndex = 4;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(640, 454);

this.Controls.Add(this.label2);

this.Controls.Add(this.groupBox1);

this.Controls.Add(this.cboPageRows);

this.Controls.Add(this.label1);

this.Controls.Add(this.dataGrid1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

this.groupBox1.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 應用程序的主入口點。

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

//給下拉框添加數據

for(int i=5;i<=10;i++)

{

this.cboPageRows.Items.Add(i);

}

if(nowPage==1)

btnBefore.Enabled=false;

LoadData();

}

private void LoadData()

{

try

{

objCn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");

objCn.Open();

objDs=new DataSet();

objAdapter=new SqlDataAdapter("select * from authors",objCn);

objCmd=objCn.CreateCommand();

objCmd.CommandText="select count(*) as c from authors";

objReader=objCmd.ExecuteReader();

while(objReader.Read())

{

count=Convert.ToInt32(objReader["c"]);//得到總的數據條數

}

objReader.Close();

//根據每頁顯示的條數計算出總***有多少頁

pageCount=count/pageRows;

if(count%pageRows>0)

{

pageCount+=1;

}

start=(nowPage-1)*pageRows;

if(nowPage>pageCount)

{

nowPage=pageCount;

}

if(nowPage<1)

{

nowPage=1;

}

//填充數據集

objAdapter.Fill(objDs,start,pageRows,"authors");

//綁定數據

this.dataGrid1.SetDataBinding(objDs,"authors");

this.label2.Text="總***有 "+count+" 條記錄"+"\r\n每頁顯示 "+pageRows+" 行記錄"+"\r\n*** "+pageCount+" 頁,當前頁數為 "+nowPage;

}

catch(SqlException ex)

{

MessageBox.Show(ex.Message);

}

finally

{

objCn.Close();

}

}

private void cboPageRows_SelectedIndexChanged(object sender, System.EventArgs e)

{

pageRows=int.Parse(cboPageRows.Text);

LoadData();

}

private void btnNext_Click(object sender, System.EventArgs e)

{

nowPage+=1;

if(nowPage>1)

this.btnBefore.Enabled=true;

if(nowPage==pageCount)

this.btnNext.Enabled=false;

LoadData();

}

private void btnBefore_Click(object sender, System.EventArgs e)

{

nowPage-=1;

if(nowPage<pageCount)

this.btnNext.Enabled=true;

if(nowPage==1)

this.btnBefore.Enabled=false;

LoadData();

}

private void btnFirst_Click(object sender, System.EventArgs e)

{

nowPage=1;

this.btnBefore.Enabled=false;

this.btnNext.Enabled=true;

LoadData();

}

private void btnLast_Click(object sender, System.EventArgs e)

{

nowPage=pageCount;

this.btnNext.Enabled=false;

this.btnBefore.Enabled=true;

LoadData();

}

}

}

這個是asp.net

頁面設計:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>數據綁定並實現分頁功能</title>

</head>

<body>

<form id="form1" runat="server">

<div>

?<table align="center" border="1" cellpadding="0" cellspacing="0" style="font-size: 9pt;

width: 403px; color: #ff0000; height: 132px; text-align: center">

<tr>

<td>

查看商品信息</td>

</tr>

<tr>

<td>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" ForeColor="#333333"

GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Right" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

代碼編寫:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

SqlConnection sqlcon;

string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=";

protected void Page_Load(object sender, EventArgs e)

{

string sqlstr = "select GoodsID as 商品ID,GoodsName as 商品名稱,GoodsIntroduce as 商品介紹,"

+ "GoodsPrice as 商品價格,GoodsIsNew as 是否推薦 from tb_GoodsInfo";

sqlcon = new SqlConnection(strCon);

SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

DataSet myds = new DataSet();

sqlcon.Open();

myda.Fill(myds, "tb_GoodsInfo");

GridView1.DataSource = myds;

GridView1.DataKeyNames = new string[] { "商品ID" };

GridView1.DataBind();

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

GridView1.DataBind();

}

}

以上均含有連庫代碼,vs2005調試成功。壹些例題,希望對樓主有幫助吧。

  • 上一篇:新HD8750M獨顯遊戲本 宏碁E1-451G首測
  • 下一篇:什麽叫做LOGO?它有多重意思嗎?
  • copyright 2024編程學習大全網