如何使用 RowDataBound 事件在数据源中的字段值显示在 GridView 控件中之前修改该值

 呈现 GridView 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 GridViewRow 对象表示)绑定到 GridView 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如修改绑定到该行的数据的值)。

GridViewRowEventArgs 对象将被传给事件处理方法,以便您可以访问正在绑定的行的属性。若要访问行中的特定单元格,请使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在绑定的是哪一种行类型(标题行、数据行等等)。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblname = (Label)e.Row.FindControl("lblname");
            Label lblmoney = (Label)e.Row.FindControl("lblmoney");
            Label lblzdot = (Label)e.Row.FindControl("lblzdot");
            Label lblsdot = (Label)e.Row.FindControl("lblsdot");
            ProfileCommon p = Profile.GetProfile(lblname.Text);
            lblmoney.Text = p.inmoney.ToString();
            lblzdot.Text = p.dcount.ToString();
            lblsdot.Text = p.scount.ToString();
        }      
    }

原文地址:https://www.cnblogs.com/ahuang1118/p/434197.html