GridView中的相关操作

 

 

在后台代码之前需要操作:

(PS:代码中需要设计相关三层架构的东西,系列合集)

Gridview 属性

ID="grdItem"  DataKeyNames="ItemID"(数据绑定的参数参照)

AutoGenerateColumns="False"  编辑模板,添加BoundField和CommandField并设置连接字段,如"ItemID";详细html源码如下所示:

<Columns>
      <asp:BoundField DataField="ItemID" HeaderText="ItemID" />
      <asp:BoundField DataField="ItemNumber" HeaderText="ItemNumber" />
      <asp:BoundField DataField="ItemName" HeaderText="ItemName" />
      <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
      <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" />
      <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
      <asp:BoundField DataField="Standard" HeaderText="Standard" />
      <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
      <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
      <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
 </Columns>

 AllowPaging="True"  PageSize="5"   //分页的设置

onpageindexchanging="grdItem_PageIndexChanging"  // 分页,GridView内部的函数,调用PageIndexChanging方法
onrowcancelingedit="grdItem_RowCancelingEdit"  //取消 写在这个方法下
 onrowdeleting="grdItem_RowDeleting"  //删除

onrowediting="grdItem_RowEditing"  //编辑
onrowupdating="grdItem_RowUpdating"  // 更新

 

后台代码如下:

 

代码
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) //判断是否是首次加载
{

ItemBind();
//非响应客户端 回发时,执行数据绑定
}

}

private void ItemBind()//定义数据绑定方法(封装)
{
ItemBLL itemBLL
= new ItemBLL();
List
<ItemInfo> itemList = itemBLL.GetAllItemList();//实例化后的itemBLl调用GetAllList()函数
if (itemList != null)
{
grdItem.DataSource
= null;
grdItem.DataSource
= itemList; //连接数据源
grdItem.DataBind();//绑定数据
}
}

protected void grdItem_RowEditing(object sender, GridViewEditEventArgs e)
{
grdItem.EditIndex
= e.NewEditIndex; //当前编辑行的索引
ItemBind(); //调用数据绑定方法
}

protected void grdItem_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ItemBLL itemBLL
= new ItemBLL();
//定义变量,获取当前编辑行的某一特定列的值
string itemNumber = (((TextBox)grdItem.Rows[e.RowIndex].Cells[1].Controls[0]).Text).ToString().Trim();
string itemName = (((TextBox)grdItem.Rows[e.RowIndex].Cells[2].Controls[0]).Text).ToString().Trim();
int categoryID = Convert.ToInt32((((TextBox)grdItem.Rows[e.RowIndex].Cells[3].Controls[0]).Text).ToString());
int SupplierID = Convert.ToInt32((((TextBox)grdItem.Rows[e.RowIndex].Cells[4].Controls[0]).Text).ToString());
double unitPrice = Convert.ToDouble((((TextBox)grdItem.Rows[e.RowIndex].Cells[5].Controls[0]).Text).ToString());
int standard = Convert.ToInt32((((TextBox)grdItem.Rows[e.RowIndex].Cells[6].Controls[0]).Text).ToString());
int itemID = Convert.ToInt32((((TextBox)grdItem.Rows[e.RowIndex].Cells[0].Controls[0]).Text).ToString());
itemBLL.UpdateItem(itemNumber, itemName, categoryID, SupplierID, unitPrice, standard, itemID);
//实例化后的itemBLL调用UpdateItem函数
grdItem.EditIndex = -1;//释放当前编辑行
ItemBind(); //重新数据绑定,一定要在在释放当期编辑行之后
}

protected void grdItem_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdItem.EditIndex
= -1;
ItemBind();
}

protected void grdItem_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ItemBLL itemBLL
= new ItemBLL();
//int itemID = Convert.ToInt32((grdItem.Rows[e.RowIndex].Cells[0].Text).ToString());
int itemID = Convert.ToInt32(grdItem.DataKeys[e.RowIndex].Value);
itemBLL.DeleteItem(itemID);
//实例化后的itemBLL调用DeleteItem函数
grdItem.EditIndex = -1;
ItemBind();
}

protected void grdItem_PageIndexChanging(object sender, GridViewPageEventArgs e) //分页功能
{
grdItem.PageIndex
= e.NewPageIndex;//获得当前编辑页面的索引
ItemBind(); //重新绑定数据
}
}
posted @ 2010-09-17 17:00  eva.xiao  阅读(206)  评论(0编辑  收藏  举报