【问题标题】:How to change in a Gridview on RowDataBound event the value of an Eval() field如何在 RowDataBound 事件的 Gridview 中更改 Eval() 字段的值
【发布时间】:2012-07-26 14:45:57
【问题描述】:

我有一个 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
  • 我猜它是空的,因为它还没有被绑定...

标签: asp.net gridview rowdatabound


【解决方案1】:
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" 是我的应用程序中的列名之一。这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多