VS2005中Gridview绑定数据库数据并分页-----更新中……
2007-04-12 20:48
357 查看
首先创建一个数据库连接类
------------------------db.cs---------------------------------------
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;
/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static System.Data.SqlClient.SqlConnection creatdb()
{
return new System.Data.SqlClient.SqlConnection("server=.;database=okweb;integrated security=true;");
}
}
-------------------------index.aspx.cs-------------------------------------
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bindtogridview();
}
}
private void bindtogridview()
{
SqlConnection conn = db.creatdb();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select articleid,title from afu_news", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.GridView1.DataKeyNames = new string[]{"articleid"};
this.GridView1.DataSource = ds.Tables["emp"];
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.bindtogridview();
}
----------------------index.aspx---------------------------------------------
<%@ 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 align="center">
<table border="0" cellpadding="0" cellspacing="0" width="780">
<tr>
<td colspan="3" style="height: 64px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 128px">
</td>
<td style="width: 47px; height: 128px">
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" Width="423px" style="font-size: 12px; vertical-align: text-bottom; list-style-type: none; text-align: left; color: black; font-family: 宋体; clear: right; float: right;" CellSpacing="1" GridLines="None" Height="50px" PageSize="5" AllowPaging=True DataKeyNames="articleid" OnPageIndexChanging="GridView1_PageIndexChanging">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle ForeColor="Black" BackColor="#DEDFDE" Height="25px" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" BorderStyle="None" Height="30px" Font-Overline="False" />
<PagerSettings FirstPageText="第一页" LastPageText="最后页" Mode="NextPreviousFirstLast"
NextPageText="下一页" PreviousPageText="上一页" />
</asp:GridView>
</td>
<td style="width: 100px; height: 128px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 153px">
</td>
<td style="width: 47px; height: 153px">
</td>
<td style="width: 100px; height: 153px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
------------------------db.cs---------------------------------------
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;
/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static System.Data.SqlClient.SqlConnection creatdb()
{
return new System.Data.SqlClient.SqlConnection("server=.;database=okweb;integrated security=true;");
}
}
-------------------------index.aspx.cs-------------------------------------
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bindtogridview();
}
}
private void bindtogridview()
{
SqlConnection conn = db.creatdb();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select articleid,title from afu_news", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.GridView1.DataKeyNames = new string[]{"articleid"};
this.GridView1.DataSource = ds.Tables["emp"];
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.bindtogridview();
}
----------------------index.aspx---------------------------------------------
<%@ 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 align="center">
<table border="0" cellpadding="0" cellspacing="0" width="780">
<tr>
<td colspan="3" style="height: 64px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 128px">
</td>
<td style="width: 47px; height: 128px">
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" Width="423px" style="font-size: 12px; vertical-align: text-bottom; list-style-type: none; text-align: left; color: black; font-family: 宋体; clear: right; float: right;" CellSpacing="1" GridLines="None" Height="50px" PageSize="5" AllowPaging=True DataKeyNames="articleid" OnPageIndexChanging="GridView1_PageIndexChanging">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle ForeColor="Black" BackColor="#DEDFDE" Height="25px" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" BorderStyle="None" Height="30px" Font-Overline="False" />
<PagerSettings FirstPageText="第一页" LastPageText="最后页" Mode="NextPreviousFirstLast"
NextPageText="下一页" PreviousPageText="上一页" />
</asp:GridView>
</td>
<td style="width: 100px; height: 128px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 153px">
</td>
<td style="width: 47px; height: 153px">
</td>
<td style="width: 100px; height: 153px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
相关文章推荐
- asp.net中使用分页控件,并绑定数据到GridView或Repeater
- GridView使用DataBind方法绑定数据时的分页问题[转]
- 我的ASPxGridView控件的数据是在后台绑定的,但是实现不了筛选和分页的功能,只能显示一页的数据?
- GridView动态绑定数据、编辑、修改、删除及分页功能
- GridView动态绑定数据、编辑、修改、删除及分页功能
- 手动绑定数据到GridView后分页的问题
- VS2005中GridView自定义列TemplateField的数据绑定技巧!
- 《GridView使用DataBind方法绑定数据时的分页问题》
- ADO 实例《创建一个临时数据库 创建一个临时数据表 GridView1绑定内容 以及Dataset_的使用》
- .net中gridview控件数据绑定及分页
- Linq查询绑定给GridView,提示 数据源不支持服务器端的数据分页.
- 界面对应字段,绑定数据到gridview 显示 及删除功能 窗体事件 分页及键
- 数据库表的转置,横向显示数据,并绑定到gridview
- Asp SqlDataSource将数据库数据绑定在 GridView
- ASPxGridview总结(ASPxGridView的增,删,改,查,数据绑定,外观显示,功能设定,分页)
- dx ASPxGridView的增,删,改,查,数据绑定,外观显示,功能设定,分页
- gridview分页情况下绑定数据时,赤字的过滤
- 在不改sql语句和数据库表的情况下,使绑定到gridview中的数据进行换行
- Linq查询绑定给GridView,提示 数据源不支持服务器端的数据分页.
- ASPxGridview总结(ASPxGridView的增,删,改,查,数据绑定,外观显示,功能设定,分页)