Search code examples
asp.netgridviewrowdatabound

How to change in a Gridview on RowDataBound event the value of an Eval() field


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


Solution

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