它不仅可以删除、更新数据,还提供了分页浏览及数据排序等功能。
自动产生字段的DataGrid控件:
下面两个例子将示范如何使用分页浏览功能,但页脚未能实现居中对齐。
例一:
.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid_Paging1.aspx.cs" Inherits="NewFolder6_Default3" %>
<!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>DataGrid实现分页浏览</title>
</head>
<body>
<h1 align="center">电脑零部件报价系统</h1>
<form runat="server">
<asp:DataGrid runat="server" ID="DataGrid1" AutoGenerateColumns="true" PageSize="8" AllowPaging="true"
HorizontalAlign="Center" OnPageIndexChanged="DataGrid_PageIndexChanged" >
<HeaderStyle Font-Size="Large" Font-Bold="true" HorizontalAlign="Center" ForeColor="#FFFFCC" BackColor="#990000" />
<ItemStyle Font-Size="Medium" HorizontalAlign="Center" ForeColor="#330099" />
<PagerStyle Font-Size="Medium" HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages" />
</asp:DataGrid>
</form>
</body>
</html>
.cs文件:
protected void BindList()
{
string constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("DB.mdb");
string sql="select * from pc";
OleDbDataAdapter da = new OleDbDataAdapter(sql,constr);
DataSet ds = new DataSet();
da.Fill(ds,"pc");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindList();
}
protected void DataGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex; //CurrentPageIndex为目前显示之分页的下标,用e.NewPageIndex来读取要改变的页
BindList();
}
效果图:
例二:
.aspx文件:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>让用户决定页码的显示格式</title>
</head>
<body>
<h1 align="center">电脑零部件报价系统</h1>
<form id="Form1" runat="server">
<asp:DataGrid runat="server" ID="DataGrid1" AutoGenerateColumns="true" PageSize="8" AllowPaging="true"
HorizontalAlign="Center" OnPageIndexChanged="DataGrid_PageIndexChanged" >
<!--AutoGenerateColumns="true"表示使用“自动产生字段模式”,DataGrid控件会自动显示SQL命令所选取出来的字段-->
<!--AllowPaging="true"表示允许分页浏览-->
<!--PageSize="8"表示每页显示8条记录-->
<HeaderStyle Font-Size="Large" Font-Bold="true" HorizontalAlign="Center" ForeColor="#FFFFCC" BackColor="#990000" />
<ItemStyle Font-Size="Medium" HorizontalAlign="Center" ForeColor="#330099" />
<PagerStyle Font-Size="Medium" HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages" />
</asp:DataGrid>
<center>
页码模式:<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Group1" Text="上下页模式" Checked="true" AutoPostBack="true"/>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Group1" Text="数字模式" Checked="false" AutoPostBack="true"/>
<!--AutoPostBack属性必须设置为true,才能使选项一变即产生效果-->
</center>
</form>
</body>
</html>
.cs文件:
protected void BindList()
{
string constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("DB.mdb");
string sql = "select * from pc";
OleDbDataAdapter da = new OleDbDataAdapter(sql, constr);
DataSet ds = new DataSet();
da.Fill(ds, "pc");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
DataGrid1.PagerStyle.Mode = PagerMode.NextPrev;
DataGrid1.PagerStyle.PrevPageText = "上一页";
DataGrid1.PagerStyle.NextPageText = "下一页";
}
else
DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
BindList();
}
protected void DataGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindList();
}
效果图:
例三:排序
.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid_Ordering.aspx.cs" Inherits="NewFolder6_DataGrid_Ordering" %>
<!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>Untitled Page</title>
</head>
<body>
<h1 align="center">电脑零部件报价系统</h1>
<form id="Form1" runat="server">
<!--OnSortCommand制定当用户点击字段标题时,执行的事件-->
<asp:DataGrid runat="server" ID="DataGrid1" HorizontalAlign="Center" AllowSorting="True" OnSortCommand="DataGrid_OnSortCommand">
<HeaderStyle Font-Size="Large" Font-Bold="true" HorizontalAlign="Center" ForeColor="#FFFFCC" BackColor="#990000" />
<ItemStyle Font-Size="Medium" HorizontalAlign="Center" ForeColor="#330099" />
</asp:DataGrid>
<!--隐藏字段,用来保存排序字段-->
<input id="Hidden1" type="hidden" runat="server" value="零部件种类 Asc"/>
</form>
</body>
</html>
.cs文件:
protected void BindList()
{
string constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("DB.mdb");
string sql = "select * from pc";
OleDbDataAdapter da = new OleDbDataAdapter(sql, constr);
DataSet ds = new DataSet();
da.Fill(ds, "pc");
DataView dv = new DataView(ds.Tables["pc"]);
dv.Sort = Hidden1.Value; //DataGrid控件虽然允许数据排序,但它并不会自动进行排序,它只可以从e.SortExpression获取排序字段,真正的排序操作是通过DataView对象的Sort属性完成的。
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindList();
}
protected void DataGrid_OnSortCommand(object sender, DataGridSortCommandEventArgs e)
{
if (Hidden1.Value.IndexOf("Desc") > 0) //字符串里是否有"Desc"子字符串
Hidden1.Value = e.SortExpression + " Asc"; //e.SortExpression获取排序字段,就是你点标题行里的哪个字段,就按哪个字段排序
else
Hidden1.Value = e.SortExpression + " Desc";
BindList();
}
protected void DataGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindList();
}
效果图:
排序前(按“编号”字段排序):
排序后(按“编号”字段排序):