2011-07-15 41 views
4

我的問題是:GridView中顯示的文本值而不是

我的表是由這個值:0, 1, 2 3

但是當gridview的負載我希望文本是顯示的,而不是僅僅這些數字。

0 = not set, 1 = low, 2 = medium, 3 = high 

我會幹出這種類似的if/else條件,但我只是想尋找一種優化的溶膠。

這裏是我的標記的GridView:

<asp:TemplateField HeaderText="Priority" SortExpression="Priority" > 
<ItemTemplate> 
<asp:Label ID="lblPriority" Text='<%# DataBinder.Eval(Container.DataItem,"Priority")%>' runat="server" /> 
</ItemTemplate> 

回答

3

假設你沒有存儲在數據庫中的任何位置顯示值,這是可以實現的渲染部分的方式。如果有人可以貢獻我會很感激,可能有一個更可維護的方式來存儲查找值。

我在記事本中寫了這個,因爲我的機器上沒有Visual Studio。請原諒,如果有任何語法錯誤。

標記:

<asp:Label ID="lblPriority" Text='<%# RenderPriority(DataBinder.Eval(Container.DataItem,"Priority")) %>' runat="server" /> 

代碼:

Protected Function RenderPriority(ByVal dbValue As Object) As String 
    Dim strReturn as String = String.Empty 
    If Not IsDbNull(dbValue) Then 
     Dim intValue as Integer 
     If Integer.TryParse(dbValue, intValue) Then 
      Select Case intValue 
       Case 0 
        strReturn = "not set" 
       Case 1 
        strReturn = "low" 
       Case 2 
        strReturn = "medium" 
       Case 3 
        strReturn = "high" 
      End Select 
     Else 
      strReturn = dbValue.ToString() 
     End If 
    End If 
    Return strReturn 
End Function 

編輯:

重新閱讀你的問題,我得到的印象後,你希望避免編寫特定的功能爲此在代碼隱藏頁面中。如果是這種情況,您應該將要與鍵值關聯的字符串存儲在數據庫中,並通過SQL語句將其拖出。或者,至少將功能降低到數據訪問層。無論哪種方式,GridView列在數據源中都會顯示所需的字符串。

2

您可以使用GridView的RowDataBound事件並在特定條件下設置該值。

下面是完整的代碼....

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     System.Data.DataRow dr = ((System.Data.DataRowView)e.Row.DataItem).Row; 

     if (dr["Priority"].ToString() == "0") 
     { 
      ((Label)e.Row.FindControl("lblPriority")).Text = "not set"; 
     } 
     else if (dr["Priority"].ToString() == "1") 
     { 
      ((Label)e.Row.FindControl("lblPriority")).Text = "low"; 
     } 
     else if (dr["Priority"].ToString() == "2") 
     { 
      ((Label)e.Row.FindControl("lblPriority")).Text = "medium"; 
     } 
     else if (dr["Priority"].ToString() == "3") 
     { 
      ((Label)e.Row.FindControl("lblPriority")).Text = "high"; 
     } 
    } 
} 
3

爲什麼不使用枚舉?這裏:

有一個名爲優先級的枚舉。然後在每個屬性上放置Description屬性,並在該屬性的構造函數中寫入顯示文本

public enum Priority 
{ 
    [Description("not set")] 
    NotSet = 0, 
    [Description("low")] 
    Low = 1, 
    [Description("medium")] 
    Medium = 2, 
    [Description("high")] 
    High = 3 
} 

然後用Enum.ToObject方法使用這些功能的數(值)轉換成其相關的顯示值:

// An extension method for ease of use that converts an integer into enum 
    public static T ToEnum<T>(this int value) 
    { 
     if (typeof(T).BaseType.Name != typeof(Enum).Name) 
     { 
      throw new Exception("Input type of generic method ToEnum<T>() is not an Enum"); 
     } 
     return (T)Enum.ToObject(typeof(T), value); 
    } 

    // Another extension method that gets the display text of the Description attribute of a given enum constant 
    public static string GetDescription(this Enum value) 
    { 
     return ((DescriptionAttribute)value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false)[0]).Description; 
    } 

然後在你的代碼,你可以寫:

databaseValue.ToEnum<Priority>().GetDescription(); 
相關問題