如何在Gridview的RowDataBound事件中更改Eval()字段的值

5

我有一个GridView:

<asp:GridView ID="gvDownloads">
   <Columns>
      <asp:TemplateField HeaderText="Status" >
         <ItemTemplate>
             <%# Eval("Enabled")%>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
<asp:GridView/>

Enabled属性是一个布尔型变量。现在我想根据Enabled属性的真/假值来显示“已启用”或“已禁用”。因此,我使用以下代码:

Sub gvDownloads_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvDownloads.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.Cells(3).Text = "True" Then
                e.Row.Cells(3).Text = "Enabled"
            Else
                e.Row.Cells(3).Text = "Disabled"
            End If

        End If

End Sub

但是它无法正常工作,因为当事件被触发时,e.Row.Cells(3).Text 是一个空字符串。我该如何解决这个问题?谢谢。


这是一个空字符串,因为在数据库中它是“NULL”吗? - mellamokb
我猜它是空的,因为它还没有被绑定... - CiccioMiami
2个回答

4
If e.Row.Cells(3).Text <> Boolean.FalseString Then
       e.Row.Cells(3).Text = "Enabled"
Else
       e.Row.Cells(3).Text = "Disabled"
End If

2

我也遇到了同样的问题。

e.Row.Cells[i].Text 是空的。我认为这个数据在绑定时尚未准备好,这有点奇怪,因为我们正在处理 RowDataBound 事件。

不过,我使用了以下方法:

     DataRowView drv = (DataRowView) e.Row.DataItem;
     if (drv["RNID"].ToString() == "")
     {
        e.Row.Visible = false;
     }

其中"RNID"是我应用程序中的一列名称。这解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接