1、利用DataGrid控件实现分页
<asp:DataGrid ID="dg" runat="server" AllowPaging="True" BorderColor="Black"
CellPadding="2" OnPageIndexChanged="ChangePage" PageSize="5">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" />
<AlternatingItemStyle BackColor="#FFFFCD" />
<HeaderStyle BackColor="#AAAADD" />
</asp:DataGrid>
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
SQL = "select * from grade";
OleDbDataAdapter da;
da = new OleDbDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
da.Fill(ds, "grade");
dg.DataSource = ds.Tables["grade"].DefaultView;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
CellPadding="2" OnPageIndexChanged="ChangePage" PageSize="5">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" />
<AlternatingItemStyle BackColor="#FFFFCD" />
<HeaderStyle BackColor="#AAAADD" />
</asp:DataGrid>
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
SQL = "select * from grade";
OleDbDataAdapter da;
da = new OleDbDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
da.Fill(ds, "grade");
dg.DataSource = ds.Tables["grade"].DefaultView;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
可以把前页和后页改成数字显示。修改为PagerStyle-Mode="NumericPages"可以实现数字显示。
一般用表格显示数据的时候,将某列显示成超级链接的形式,用来进一步显示该记录的详细信息。
利用HyperLinkColumn控件设置显示链接的列,DataNavigateUrlField属性表示链接的字段,DataNavigateUrlFormatString属性表示链接的地址。
<asp:DataGrid ID="dg" runat="server" AllowPaging="True" BorderColor="Black"
CellPadding="2" OnPageIndexChanged="ChangePage" PageSize="5" AutoGenerateColumns="False">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" Mode="NumericPages" />
<AlternatingItemStyle BackColor="#FFFFCD" />
<HeaderStyle BackColor="#AAAADD" />
<Columns>
<asp:HyperLinkColumn DataTextField="学号" HeaderText="学号"
DataNavigateUrlField="学号"
DataNavigateUrlFormatString="test.aspx?id={0}"
Target="_blank" />
<asp:BoundColumn DataField="姓名" HeaderText="姓名" />
<asp:BoundColumn DataField="数学" HeaderText="数学成绩" ItemStyle-HorizontalAlign="Right"/>
</Columns>
</asp:DataGrid>
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
SQL = "select * from grade";
OleDbDataAdapter da;
da = new OleDbDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
da.Fill(ds, "grade");
dg.DataSource = ds.Tables["grade"].DefaultView;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
//当单击某一行超级链接时,自动弹出新网页,调用程序test.aspx
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection Conn;
Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("person.mdb"));
Conn.Open();
string strSQL = "select * from grade where 学号="+Request["id"];
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
OleDbDataReader dr = Comm.ExecuteReader();
string html = " ";
while (dr.Read())
{
html += "<h1>学员:";
html += "<font color=red>"+dr["姓名"].ToString()+"</font>的成绩为:</h1><br>";
html += "学号为:"+dr["学号"].ToString()+"<br>";
html += "数学成绩为:"+dr["数学"].ToString()+"<br>";
html += "";
}
Response.Write(html);
Conn.Close();
}
CellPadding="2" OnPageIndexChanged="ChangePage" PageSize="5" AutoGenerateColumns="False">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" Mode="NumericPages" />
<AlternatingItemStyle BackColor="#FFFFCD" />
<HeaderStyle BackColor="#AAAADD" />
<Columns>
<asp:HyperLinkColumn DataTextField="学号" HeaderText="学号"
DataNavigateUrlField="学号"
DataNavigateUrlFormatString="test.aspx?id={0}"
Target="_blank" />
<asp:BoundColumn DataField="姓名" HeaderText="姓名" />
<asp:BoundColumn DataField="数学" HeaderText="数学成绩" ItemStyle-HorizontalAlign="Right"/>
</Columns>
</asp:DataGrid>
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
SQL = "select * from grade";
OleDbDataAdapter da;
da = new OleDbDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
da.Fill(ds, "grade");
dg.DataSource = ds.Tables["grade"].DefaultView;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
//当单击某一行超级链接时,自动弹出新网页,调用程序test.aspx
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection Conn;
Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("person.mdb"));
Conn.Open();
string strSQL = "select * from grade where 学号="+Request["id"];
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
OleDbDataReader dr = Comm.ExecuteReader();
string html = " ";
while (dr.Read())
{
html += "<h1>学员:";
html += "<font color=red>"+dr["姓名"].ToString()+"</font>的成绩为:</h1><br>";
html += "学号为:"+dr["学号"].ToString()+"<br>";
html += "数学成绩为:"+dr["数学"].ToString()+"<br>";
html += "";
}
Response.Write(html);
Conn.Close();
}
2、利用DataGrid控件动态添加数据
public partial class _Default : System.Web.UI.Page
{
OleDbConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
Conn = new OleDbConnection(ConnStr);
if (!IsPostBack) BindGrid();
}
private void BindGrid()
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from grade", Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
public void Add_Click(object sender, EventArgs E)
{
String strSQL = "insert into grade values(" + id.Text + ",'" + sex.Text + "','" + name.Text + "'," +
yuwen.Text+","+math.Text+","+ english.Text+")";
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
Comm.ExecuteNonQuery();
Message.InnerHtml = "<b>添加成功</b>";
}
catch (OleDbException)
{
Message.InnerHtml = "添加失败";
Message.Style["color"] = "red";
}
Conn.Close();
BindGrid();
}
}
<form id="form1" runat="server">
<table width="95%">
<tr>
<td valign="top">
<asp:DataGrid ID="dg" runat="server" BorderColor="Black" CellPadding="3"
OnPageIndexChanged="ChangePage" PageSize="5" AutoGenerateColumns="False" BackColor="#CCCCFF" Font-Names="Verdana" Font-Size="9pt" Width="400px">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" Mode="NumericPages" />
<HeaderStyle BackColor="#AAAADD" />
<Columns>
<asp:BoundColumn DataField="学号" HeaderText="学号" />
<asp:BoundColumn DataField="性别" HeaderText="性别" />
<asp:BoundColumn DataField="姓名" HeaderText="姓名" />
<asp:BoundColumn DataField="语文" HeaderText="语文" />
<asp:BoundColumn DataField="数学" HeaderText="数学" />
<asp:BoundColumn DataField="英语" HeaderText="英语" />
</Columns>
</asp:DataGrid>
</td>
<td valign="top">
<table style="font:8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">添加新的学员:
</td>
</tr>
<tr><td nowrap>学号:</td>
<td><asp:TextBox ID="id" runat="server" /></td>
</tr>
<tr><td nowrap>性别:</td>
<td><asp:TextBox ID="sex" runat="server" /></td>
</tr>
<tr><td nowrap>姓名:</td>
<td><asp:TextBox ID="name" runat="server" /></td>
</tr>
<tr><td nowrap>语文:</td>
<td><asp:TextBox ID="yuwen" runat="server" /></td>
</tr>
<tr><td nowrap>数学:</td>
<td><asp:TextBox ID="math" runat="server" /></td>
</tr>
<tr><td nowrap>英语:</td>
<td><asp:TextBox ID="english" runat="server" /></td>
</tr>
<tr><td colspan="2" style="padding-top:15" align="center">
<asp:Button Text="添加" OnClick="Add_Click" runat="Server" /></td>
</tr>
<tr><td colspan="2" style="padding-top:15" align="center">
<span id="Message" style="font:arial 11pt;" runat="server" /></td></tr>
</table>
</td>
</tr>
</table>
</form>
3、使用DataGrid控件动态操作数据{
OleDbConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
string Provider, DataBase, ConnStr, SQL;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
Conn = new OleDbConnection(ConnStr);
if (!IsPostBack) BindGrid();
}
private void BindGrid()
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from grade", Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
public void ChangePage(object sender, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
public void Add_Click(object sender, EventArgs E)
{
String strSQL = "insert into grade values(" + id.Text + ",'" + sex.Text + "','" + name.Text + "'," +
yuwen.Text+","+math.Text+","+ english.Text+")";
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
Comm.ExecuteNonQuery();
Message.InnerHtml = "<b>添加成功</b>";
}
catch (OleDbException)
{
Message.InnerHtml = "添加失败";
Message.Style["color"] = "red";
}
Conn.Close();
BindGrid();
}
}
<form id="form1" runat="server">
<table width="95%">
<tr>
<td valign="top">
<asp:DataGrid ID="dg" runat="server" BorderColor="Black" CellPadding="3"
OnPageIndexChanged="ChangePage" PageSize="5" AutoGenerateColumns="False" BackColor="#CCCCFF" Font-Names="Verdana" Font-Size="9pt" Width="400px">
<PagerStyle HorizontalAlign="Right" NextPageText="后页" PrevPageText="前页" Mode="NumericPages" />
<HeaderStyle BackColor="#AAAADD" />
<Columns>
<asp:BoundColumn DataField="学号" HeaderText="学号" />
<asp:BoundColumn DataField="性别" HeaderText="性别" />
<asp:BoundColumn DataField="姓名" HeaderText="姓名" />
<asp:BoundColumn DataField="语文" HeaderText="语文" />
<asp:BoundColumn DataField="数学" HeaderText="数学" />
<asp:BoundColumn DataField="英语" HeaderText="英语" />
</Columns>
</asp:DataGrid>
</td>
<td valign="top">
<table style="font:8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">添加新的学员:
</td>
</tr>
<tr><td nowrap>学号:</td>
<td><asp:TextBox ID="id" runat="server" /></td>
</tr>
<tr><td nowrap>性别:</td>
<td><asp:TextBox ID="sex" runat="server" /></td>
</tr>
<tr><td nowrap>姓名:</td>
<td><asp:TextBox ID="name" runat="server" /></td>
</tr>
<tr><td nowrap>语文:</td>
<td><asp:TextBox ID="yuwen" runat="server" /></td>
</tr>
<tr><td nowrap>数学:</td>
<td><asp:TextBox ID="math" runat="server" /></td>
</tr>
<tr><td nowrap>英语:</td>
<td><asp:TextBox ID="english" runat="server" /></td>
</tr>
<tr><td colspan="2" style="padding-top:15" align="center">
<asp:Button Text="添加" OnClick="Add_Click" runat="Server" /></td>
</tr>
<tr><td colspan="2" style="padding-top:15" align="center">
<span id="Message" style="font:arial 11pt;" runat="server" /></td></tr>
</table>
</td>
</tr>
</table>
</form>
public partial class _Default : System.Web.UI.Page
{
OleDbConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
string Provider, DataBase, ConnStr;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
Conn = new OleDbConnection(ConnStr);
if (!IsPostBack) BindGrid();
}
private void BindGrid()
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from grade order by 学号", Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
public void DataGrid_Edit(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
public void DataGrid_Cancel(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
BindGrid();
}
public void DataGrid_Update(object sender, DataGridCommandEventArgs e)
{
string strSQL = "update grade set " +
"姓名='" + ((TextBox)e.Item.Cells[3].Controls[0]).Text
+ "',数学=" + ((TextBox)e.Item.Cells[4].Controls[0]).Text
+ " where 学号=" + dg.DataKeys[(int)e.Item.ItemIndex];
OleDbCommand cm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>编辑成功</b>";
dg.EditItemIndex = -1;
}
catch(OleDbException)
{
Message.InnerHtml="编辑失败";
Message.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();
}
public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
{
string strSQL = "delete from grade where 学号="
+ dg.DataKeys[(int)e.Item.ItemIndex];
OleDbCommand cm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>删除成功</b>";
}
catch (OleDbException)
{
Message.InnerHtml = "<b>删除失败</b>";
Message.Style["color"]="red";
}
Conn.Close();
BindGrid();
}
}
<form id="form1" runat="server">
<asp:DataGrid ID="dg" runat="server" Width="400" BackColor="#ccccff" BorderColor="black"
ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd" DataKeyField="学号" AutoGenerateColumns="false"
OnEditCommand="DataGrid_Edit"
OnCancelCommand="DataGrid_Cancel"
OnUpdateCommand="DataGrid_Update"
OnDeleteCommand="DataGrid_Delete">
<Columns>
<asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false" />
<asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="学号" DataField="学号" ReadOnly="true" />
<asp:BoundColumn HeaderText="姓名" DataField="姓名" />
<asp:BoundColumn HeaderText="数学" DataField="数学" />
</Columns>
</asp:DataGrid>
<span id="Message" style="font:arial 11pt;" runat="Server" />
</form>
{
OleDbConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
string Provider, DataBase, ConnStr;
Provider = "Microsoft.Jet.OLEDB.4.0;";
DataBase = Server.MapPath("person.mdb");
ConnStr = "Provider=" + Provider + "Data Source=" + DataBase;
Conn = new OleDbConnection(ConnStr);
if (!IsPostBack) BindGrid();
}
private void BindGrid()
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from grade order by 学号", Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
public void DataGrid_Edit(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
public void DataGrid_Cancel(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
BindGrid();
}
public void DataGrid_Update(object sender, DataGridCommandEventArgs e)
{
string strSQL = "update grade set " +
"姓名='" + ((TextBox)e.Item.Cells[3].Controls[0]).Text
+ "',数学=" + ((TextBox)e.Item.Cells[4].Controls[0]).Text
+ " where 学号=" + dg.DataKeys[(int)e.Item.ItemIndex];
OleDbCommand cm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>编辑成功</b>";
dg.EditItemIndex = -1;
}
catch(OleDbException)
{
Message.InnerHtml="编辑失败";
Message.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();
}
public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
{
string strSQL = "delete from grade where 学号="
+ dg.DataKeys[(int)e.Item.ItemIndex];
OleDbCommand cm = new OleDbCommand(strSQL, Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>删除成功</b>";
}
catch (OleDbException)
{
Message.InnerHtml = "<b>删除失败</b>";
Message.Style["color"]="red";
}
Conn.Close();
BindGrid();
}
}
<form id="form1" runat="server">
<asp:DataGrid ID="dg" runat="server" Width="400" BackColor="#ccccff" BorderColor="black"
ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd" DataKeyField="学号" AutoGenerateColumns="false"
OnEditCommand="DataGrid_Edit"
OnCancelCommand="DataGrid_Cancel"
OnUpdateCommand="DataGrid_Update"
OnDeleteCommand="DataGrid_Delete">
<Columns>
<asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false" />
<asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="学号" DataField="学号" ReadOnly="true" />
<asp:BoundColumn HeaderText="姓名" DataField="姓名" />
<asp:BoundColumn HeaderText="数学" DataField="数学" />
</Columns>
</asp:DataGrid>
<span id="Message" style="font:arial 11pt;" runat="Server" />
</form>
程序中利用ItemStyle-Wrap="false"语句设置不折行显示,利用DataKeyField="学号"语句设置关键字段是学号,因此可以利用dg.DataKeys[(int)e.item.ItemIndex]取出某行的学号。取其行的其它列时,使用((TextBox)e.Item.Cells[3].Controls[0]).Text语句,e中存储当前的所有对象,通过表格和控件的定位,得到文本框的值。