简体   繁体   中英

Modify datasource row values during databind in gridview

I have a grid view which I am binding with data table. My problem is data table has integer values as 1,2,3,4,5. For all these values I want to bind A,B,C,D,E respectively in grid view. I am using bound fields. I dont know where to modify data coming from data table??

Make that column to Template column and put label

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

and then you do it in RowDataBound event of Gridview

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";
    }
     ................
      ................
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM