Microsoft Developer
Microsoft Developer

Reputation: 5459

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??

Upvotes: 2

Views: 8821

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

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

Upvotes: 5

Related Questions