2015-05-20 40 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代碼段,以及您的字段映射嗎? –