- 陶小凡
-
.aspx 中的DataGrid 控件代码:(我以前做的,,没改行列,,自己看看,,改他挺简单的)
<asp:datagrid
id="ProductItemList"
runat="server"
BorderColor="#E0E0E0"
Width="80%"
AutoGenerateColumns="False"
BorderStyle="Solid"
OnDeleteCommand="Delete"
DataKeyField="ProductId"
AllowSorting="True"
OnSortCommand="Sort">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle
HorizontalAlign="Center"
Height="20px"
BackColor="#ECEDF0">
</HeaderStyle>
<Columns>
<asp:BoundColumn
DataField="ProductId"
HeaderText="Product Id"
SortExpression="ProductId">
</asp:BoundColumn>
<asp:BoundColumn
DataField="SortName"
HeaderText="Sort Name"
SortExpression="SortName">
</asp:BoundColumn>
<asp:BoundColumn
DataField="ProductName"
HeaderText="Product Name"
SortExpression="ProductName">
</asp:BoundColumn>
<asp:BoundColumn
DataField="ProductPrice"
HeaderText="Product Price"
DataFormatString="{0:C}"
SortExpression="ProductPrice">
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Edit">
<ItemStyle
HorizontalAlign="Center"
Width="100px"
CssClass="grid-edit-column">
</ItemStyle>
<ItemTemplate>
<a
href="ProductUpdate.aspx?ProductId=<%#DataBinder.Eval(Container.DataItem,"
ProductId")%>">
<img src="../images/icon-pencil.gif" border="0">
</a>
<img src="../images/shim.gif" width="6">
<asp:imagebutton
Runat="server"
ImageUrl="../images/icon-delete.gif"
CommandName="Delete"
CausesValidation="False"
ID="CatDeleteButton">
</asp:imagebutton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
.aspx.cs中控件、变量的声明和方法的定义
protected System.Web.UI.WebControls.DataGrid ProductItemList;
protected iProduct.Business.Product myProduct = new
iProduct.Business.Product();
protected string SortField;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
if (SortField == "")
{
SortField = "ProductId";
}
BindProduct();
}
}
private void BindProduct()
{
DataSet ds = myProduct.GetAllWithSortName();
DataTable dt = ds.Tables["iproduct_product"];
DataView dv = new DataView(dt);
dv.Sort = SortField;
ProductItemList.DataSource = dv;
ProductItemList.DataBind();
}
//删除记录
public void Delete(Object sender, DataGridCommandEventArgs e)
{
( ProductItemList.DataKeys[(int)e.Item.ItemIndex] );
myProduct.Delete();
BindProduct();
}
//数据排序方法
protected void Sort(Object sender, DataGridSortCommandEventArgs e)
{
SortField = (string)e.SortExpression;
BindProduct();
}