gridview显示文本而不是值 [英] gridview display the text instead of values

查看:186
本文介绍了gridview显示文本而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:

我的表格由以下值组成: 0,1,2 3



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

  0 =未设置,1 =低,2 =中等,3 =高

我可以这样做,如果/其他条件,但我只是想寻求一个优化溶胶。



这里是我的标记gridview:

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


解决方案

假设您没有存储的显示值数据库在任何地方,这是一种可以实现渲染部分的方式。如果任何人可以贡献我会很感激它。



我在记事本中写了这个,因为我没有Visual我的机器上的工作室。请原谅我是否有任何语法错误。



标记:

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

代码:

  Protected Function RenderPriority(ByVal dbValue As Object)As String 
Dim strReturn as String = String.Empty
If IsDbNull(dbValue)Then
Dim intValue as Integer
如果Integer.TryParse(dbValue,intValue)则
选择案例intValue
案例0
strReturn =未设置
案例1
strReturn =低
案例2
strReturn =中
案例3
strReturn =高
结束选择
其他
strReturn = dbValue.ToString( )
结束如果
结束如果
返回str返回
结束功能






编辑:

重新阅读您的问题后,我得到的印象是您宁愿避免编写特定功能为此在代码隐藏页面中。如果是这种情况,您应该将要与键值关联的字符串存储在数据库中,并通过SQL语句将其拖出。或者,至少将功能降低到数据访问层。无论哪种方式,> GridView 列都将通过其数据源显示所需的字符串。


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>

解决方案

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.

这篇关于gridview显示文本而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆