5

I have a GridView:

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

The Enabled property is a boolean. Now I would like to display Enabled/Disabled based on True/False of the Enabled property. Therefore I use:

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

But it does not work since when the event is launched e.Row.Cells(3).Text is an empty string. How can I solve this problem? Thanks

CiccioMiami
  • 8,028
  • 32
  • 90
  • 151

2 Answers2

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
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
2

Same problem with me.

e.Row.Cells[i].Text was empty. I think the data is not bound at the time which is somehow weird since we are in RowDataBound event.

However, I used:

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

where "RNID" is one of the column names in my application. This solved my problem.

sloth
  • 99,095
  • 21
  • 171
  • 219