开发者

gridview display the text instead of values

开发者 https://www.devze.com 2023-03-20 06:02 出处:网络
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.

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 = no开发者_如何学运维t 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>


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.


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();


You can use the RowDataBound event of the GridView and set the value on specific condition.

Here is the complete code....

 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";
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号