在gridview中的数据绑定期间修改数据源行值

时间:2011-06-08 09:30:55

标签: c# .net asp.net gridview

我有一个网格视图,我与数据表绑定。我的问题是数据表的整数值为1,2,3,4,5.对于所有这些值,我想分别在网格视图中绑定A,B,C,D,E。我正在使用绑定字段。我不知道在哪里修改来自数据表的数据??

1 个答案:

答案 0 :(得分:5)

将该列设置为“模板”列并放置标签

<asp:TemplateField HeaderText="HeaderText">
 <ItemTemplate>
 <asp:Label ID="lbl" runat="server" ></asp:Label>
</ItemTemplate>

然后在Gridview的RowDataBound事件中执行此操作

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    DataRow dr = ((DataRowView)e.Row.DataItem).Row;
    if(dr["ColumnName"].ToString() == "1" )
    {
      ((Label)e.Row.FindControl("lbl")).Text = "A";
    }
    else if(dr["ColumnName"].ToString() == "2" )
    {
      ((Label)e.Row.FindControl("lbl")).Text = "B";
    }
     ................
      ................
   }
}
相关问题