1.GridView中如果有隐藏列的话,通过这种方式值取不到值的:
this.GridView.Rows[e.NewEditIndex].Cells[2].Text
所以必须通过其他的方式来取值:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果DataKeyNames有多个值(DataKeyNames="ID,ProjectState,PriceState")
//可以通过以下方式来取值
this.GridView_Price.DataKeys[e.Row.RowIndex].Values[0].ToString();
this.GridView_Price.DataKeys[e.Row.RowIndex].Values[1].ToString();
this.GridView_Price.DataKeys[e.Row.RowIndex].Values[2].ToString();
}
2.GridView的编辑操作
<asp:GridView ID="GridView_Cost" OnRowEditing="GridView_Cost_RowEditing" OnRowCancelingEdit="GridView_Cost_RowCancelingEdit" OnRowUpdating="GridView_Cost_RowUpdating" >
/// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView_Cost_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView_Cost.EditIndex = e.NewEditIndex;
DataBind_Project(proId, 0);
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView_Cost_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView_Cost.EditIndex = -1;
DataBind_Project(proId, 0);
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView_Cost_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ProjectCost proCost=new ProjectCost();
proCost.ID = Convert.ToInt32(this.GridView_Cost.DataKeys[e.RowIndex].Value.ToString());
proCost.ProjectCostPrice=Convert.ToDecimal(((TextBox)(this.GridView_Cost.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
ServiceLocator.ProjectService.UpdateProjectCost(proCost);
this.GridView_Cost.EditIndex = -1;
DataBind_Project(proId, 0);
}