2011-07-15 7 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

どこでもDBに格納された表示値を持っていないと仮定すると、これはあなたがレンダリング部分を実装することができる方法です。ルックアップ値を保存するためのメンテナンス可能な方法があるかもしれません。

私はマシンに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 

編集:

私はあなたが特定の機能を書き込むことを避けるために好む印象を得る再読み込みあなたの質問の後この目的のために、コードビハインドページで。そのような場合は、キー値に関連付ける文字列をDBに格納し、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

列挙を使用していないのはなぜ?ここでは:

Priorityという列挙体があります。その後、それぞれに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(); 
関連する問題