Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gridview display the text instead of values

my question is:

my table consists of this values: 0, 1, 2 3

but when the gridview loads i want the text to be display instead of just those numbers.

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

i could have done this like if/else condition but i just wanted to seek for a optimized sol.

here is my markup gridview:

<asp:TemplateField  HeaderText="Priority" SortExpression="Priority" >
<ItemTemplate>
 <asp:Label ID="lblPriority" Text='<%# DataBinder.Eval(Container.DataItem,"Priority")%>' runat="server" />
 </ItemTemplate>
like image 695
Nick Kahn Avatar asked Jan 19 '23 05:01

Nick Kahn


2 Answers

Assuming you don't have the display values stored in the DB anywhere, this is a way you can implement the rendering part. There may be a more maintainable way to store the lookup values, if anyone could contribute I'd appreciate it.

I wrote this in notepad since I don't have Visual Studio on my machine. Please excuse me if there are any syntax errors.

Markup:

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

Code:

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

Edit:

After re-reading your question I get the impression you would prefer to avoid writing a specific function for this purpose in the code-behind page. If that is the case you should probably store the strings you want associated with the key values in the DB and pull them out through your SQL statement. Or, at the very least push the functionality down into a Data Access Layer. Either way ideally the GridView column will be presented with the required string by its datasource.

like image 50
pseudocoder Avatar answered Jan 30 '23 23:01

pseudocoder


Why not using enumerations? Here:

Have an enumeration called Priority. Then put Description attribute on each of them, and write the display text inside the constructor of that attribute.

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

Then use Enum.ToObject method to convert the numbers (values) into their associated display value using these functions:

    // 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;
    }

Then in your code, you can write:

databaseValue.ToEnum<Priority>().GetDescription();
like image 27
Saeed Neamati Avatar answered Jan 31 '23 01:01

Saeed Neamati