GridView中如何取隐藏列的值

 

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);
    }

  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值