这篇文章主要介绍如何在DataGrid控件中实现编辑、删除、分类以及分页操作。为了实现我们的意图,我们使用SqlServer2000自带的NorthWind数据库。程序分为两部分:
1.包含HTML代码的.ASPX文件
2.包含所有逻辑及方法的后台C#类文件
代码:
ASPX文件:
在这里我们设计了一个DataGrid对象,我为一些属性和方法作了注解。它就变得如此的简单:
<asp:DataGrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 104px" runat="server"
BorderStyle="Ridge"
GridLines="None"
BorderWidth="2px"
BorderColor="White"
BackColor="White"
CellPadding="3"
CellSpacing="1"
AllowPaging="True" // AllowPaging属性的"True"时, 可进行分页操作
AllowSorting="True" // 这是分类属性
PageSize="15" //设每页25条记录
PagerStyle-Mode="NextPrev" //有2种模式风格:next previous和page numberin
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Previous"
PagerStyle-HorizontalAlign="Center"
PagerStyle-Position="TopAndBottom"
DataKeyField="ProductID" // DataGrid的每条记录都包含一个ProductID字段
OnPageIndexChanged="MyDataGrid_PageIndexChanged" // 当用户进行翻页操作时就激活MyDataGrid_PageIndexChanged函数(function)
OnSortCommand="Sort_Grid" //当用户对DataGrid分类时激活Sort_Grid(function)函数
OnDeleteCommand="MyDataGrid_Delete" //这一事件激活MyDataGrid_Delete函数(function)删除一条记录
OnUpdateCommand="MyDataGrid_Update" //这一事件激活MyDataGrid_Update函数(function)更新一条记录
OnCancelCommand="MyDataGrid_Cancel //这一事件激活MyDataGrid_Cancel函数(function)取消当前操作
OnEditCommand="MyDataGrid_Edit" //这一事件激活MyDataGrid_Edit函数(function)编辑一条记录
AutoGenerateColumns="False" // 设置自动产生行为"False"
HorizontalAlign="Left">
<FooterStyle ForeColor= "Black"BackColor="#C6C3C6"></FooterStyle>
<HeaderStyle Font-Bold= "True" ForeColor= "#E7E7FF"BackColor="#4A3C8C"></HeaderStyle>
<PagerStyle NextPageText="Next" PrevPageText="Previous" HorizontalAlign="Right" ForeColor="Black"
Position= "TopAndBottom"BackColor="#C6C3C6"></PagerStyle>
<SelectedItemStyle Font-Bold= "True" ForeColor= "White"BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor= "Black"BackColor="#DEDFDE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText= "<img border=0 src=ok.gif?http://www.xvna.com>" CancelText= "<imgborder=0 src=cancel.gif?http://www.xvna.com>" EditText= "<imgborder=0src=edit.gif?http://www.xvna.com>"></asp:EditCommandColumn>
<asp:ButtonColumn Text= "<img border= 0src= delete.gif?http://www.xvna.com>"CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField= "ProductID" SortExpression="ProductID" ReadOnly="True" HeaderText= "ProductID"></asp:BoundColumn>
<asp:BoundColumn DataField= "ProductName" SortExpression="ProductName" HeaderText= "ProductName"></asp:BoundColumn>
<asp:BoundColumn DataField="QuantityPerUnit" SortExpression="QuantityPerUnit" HeaderText= "Quantity PerUnit"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitPrice" SortExpression="UnitPrice" HeaderText= "Unit Price"DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsInStock" SortExpression="UnitsInStock" HeaderText= "Units InStock"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsOnOrder" SortExpression="UnitsOnOrder" HeaderText= "Units OnOrder"></asp:BoundColumn>
<asp:BoundColumn DataField= "ReorderLevel" SortExpression="ReorderLevel" HeaderText= "ReorderLevel"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Discontinued" SortExpression="Discontinued">
<ItemTemplate>
<asp:CheckBox id= "Discontinued" runat="server" Checked= '<%# DataBinder.Eval(Container.DataItem, "Discontinued")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
你看,是不是不难?关键在于我们常动手动脑。多看资料也很关键哦!
C#后台程序:
让我们先看一段程序:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}