【问题标题】:gridview display the text instead of valuesgridview 显示文本而不是值
【发布时间】:2011-07-15 02:24:51
【问题描述】:

我的问题是:

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

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

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

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

这是我的标记网格视图:

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

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    假设您没有将显示值存储在任何地方的数据库中,这是您可以实现渲染部分的一种方式。可能有一种更易于维护的方式来存储查找值,如果有人可以贡献我将不胜感激。

    我在记事本中写了这个,因为我的机器上没有 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 列都将通过其数据源显示所需的字符串。

    【讨论】:

      【解决方案2】:

      为什么不使用枚举?这里:

      有一个称为优先级的枚举。然后将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();
      

      【讨论】:

        【解决方案3】:

        您可以使用 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";
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-03-17
          • 1970-01-01
          • 2011-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-30
          • 1970-01-01
          相关资源
          最近更新 更多