This sample code will help you to perform hide and other operation at row level inside the Gridview RowCreated Event.
ASP.NET Code:
<asp:GridView ID="GridViewAllNews" runat="server" DataSourceID="ObjectDataSourceAllNews"
OnRowCreated="GridViewAllNews_RowCreated" DataKeyNames="Id" >
First check if EventArgs is null just return. always check for RowType is DataRow and DataItem is not null.
Find the control using
Image newsImage = (Image)e.Row.FindControl("Image2");
and set the visibility of cell.
C# code:
protected void GridViewAllNews_RowCreated(object source, GridViewRowEventArgs e)
{
if (e.Row == null) return;
// display if 'Visible' = true
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
{
bool visible = (bool)DataBinder.Eval(e.Row.DataItem, "Visible");
e.Row.Visible = visible;
}
// display image if a url is specified
string imageUrl = (string)DataBinder.Eval(e.Row.DataItem, "ImageUrl");
if (String.Empty.Equals(imageUrl))
{
Image newsImage = (Image)e.Row.FindControl("Image2");
if (newsImage != null)
newsImage.Visible = false;
}
}
1 comments: