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



我有一个GridView:

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

Enabled属性是布尔值。现在,我想显示基于Enabled属性的True/False的Enabled/Disabled。因此我使用:

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是一个空字符串。我该如何解决这个问题?感谢

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

我也有同样的问题。

e.Row.Cells[i].Text为空。我认为数据当时没有绑定,这有点奇怪,因为我们在RowDataBound事件中。

然而,我使用了:

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

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

最新更新