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
If e.Row.Cells(3).Text <> Boolean.FalseString Then
e.Row.Cells(3).Text = "Enabled"
Else
e.Row.Cells(3).Text = "Disabled"
End If
Cem Kandemir
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