Gridview displays text instead of values

my question is:

my table consists of the following values: 0, 1, 2 3

but when loading gridview I want the text to appear instead of these numbers.

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

I could do this as an if / else condition, but I just wanted to find optimized evils.

here is my gridview markup:

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

Assuming you don’t have display values ​​stored in the database, this is a way to implement part of the rendering. There might be a more convenient way to store search values, if someone could contribute, I would appreciate it.

I wrote this in notepad since I do not have Visual Studio on my machine. Sorry if there are 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 reading your question again, I get the impression that you prefer not to write a specific function for this purpose on the code page. If so, you should probably store the rows you want to associate with the key values ​​in the database, and pull them through the SQL statement. Or at least push functionality to the data access layer. In any case, ideally, the GridView column will be represented by the required row using the data source.

+3
source

Why not use enumerations? Here:

Have an listing called Priority. Then add the Description attribute for each of them and write the display text inside the constructor of this attribute.

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

Then use the Enum.ToObject method to convert numbers (values) to their associated display value using the following 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(); 
+3
source

You can use the RowDataBound GridView event and set a value for a specific condition.

Here is the full 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"; } } } 
+2
source

All Articles