2012-07-26 57 views
5

的價值我有一個GridView:如何在GridView改變RowDataBound事件一個eval()字段

<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空字符串。我怎麼解決這個問題?謝謝

+0

它是一個空字符串,因爲它在數據庫中是NULL的嗎? – mellamokb 2012-07-26 14:55:25

+0

將其與1比較? – 2012-07-26 14:57:52

+0

我想這是空的,因爲它還沒有被綁定... – CiccioMiami 2012-07-26 15:48:35

回答

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"是在我的應用程序列的名字之一。這解決了我的問題。

相關問題