Stoyan Petkov
Stoyan Petkov

Reputation: 491

How to convert and show cell/label value from int to string

I have ASP.NET project. It has GridView which is populated from my db. In the db i have column named Role and its type is integer. In DataGrid my column is TemplateField. This is how my GridView looks like

How i can show on Admin or Guest instead of 1 or 2 ?

Upvotes: 1

Views: 1785

Answers (1)

S.Krishna
S.Krishna

Reputation: 876

You can use OnRowDataBound in your ASPX and handle it in your CS file. Sample below

In your ASPX File

<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound"></asp:GridView>

In your CS File

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Cell#5 because your Role appears in that field (Index starts with 0)
        if(e.Row.Cells[5].Text.Equals(1))
            e.Row.Cells[5].Text = "Admin";
        if(e.Row.Cells[5].Text.Equals(2))
            e.Row.Cells[5].Text = "Guest";
    }
}

Upvotes: 3

Related Questions