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

风流意气都作罢 提交于 2019-12-01 17:37:20

问题


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


回答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:


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.



来源:https://stackoverflow.com/questions/11671905/how-to-change-in-a-gridview-on-rowdatabound-event-the-value-of-an-eval-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!