2015-05-20 36 views
1

我有ASP.NET项目。它有从我的数据库填充的GridView。在数据库我有名为角色列,其类型是整数。在DataGrid中,我的列是TemplateField。 This is how my GridView looks like如何将int和string/label的值转换为字符串

我如何显示在管理员或访客而不是1或2?

+0

您使用的标签,以显示1对或2或其他任何列'Role'下的模板文件场? –

+0

如果1然后'管理员',你可以使'SQL查询'使用'case'else如果'2'使用'OnRowDataBound'设置值另一种方式设置'Guest' –

+0

我使用标签来显示角色,但后来我将使用下拉列表 –

回答

3

您可以在ASPX中使用OnRowDataBound,并在CS文件中处理它。下面的示例

在您的ASPX文件

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

在你的CS

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"; 
    } 
} 
+0

谢谢这是使用完整。 –

+0

RowDataBound方法存在一个问题,文本总是“”(空字符串)为什么 –

+0

您可以发布您的ASPX代码段,以及您的字段映射吗? –