gridview显示文本而不是值

时间:2011-07-15 02:24:52

标签: asp.net gridview

我的问题是:

我的表格包含以下值:0, 1, 2 3

但是当gridview加载时我希望显示文本而不是那些数字。

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

我可以像if / else条件那样做,但我只是想寻找一个优化的sol。

这是我的标记gridview:

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

3 个答案:

答案 0 :(得分: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列都将通过其数据源显示所需的字符串。

答案 1 :(得分: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();

答案 2 :(得分: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";
        }
    }
}