I'm trying to hide/show different columns on a nested telerik rad grid in the pre-render event, based on a flag but they are all displaying regardless of what I set the visible or display property to
These are my grid columns:
<telerik:GridBoundColumn UniqueName="LocalDeanery" ReadOnly="true" DataField="localdeanery" HeaderText="Local Deanery" SortExpression="localdeanery"
HtmlEncode="false" >
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Sector2013" HeaderText="Sector" SortExpression="hasBeenHeld desc,Sector" AllowFiltering="false" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Sector").ToString() + " (" + Eval("PrefRank").ToString() + ") " + (Eval("conditional").ToString()==""?"":"Conditional")%>'
Visible='<%# Eval("OfferId").ToString()!="" %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn UniqueName="Region" ReadOnly="true" DataField="tRegion" HeaderText="Region" SortExpression="tRegion"
HtmlEncode="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Subregion" ReadOnly="true" DataField="tSubRegion" HeaderText="Sub-Region" SortExpression="tSubRegion"
HtmlEncode="false" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Sector2014" ReadOnly="true" DataField="tSector" HeaderText="Sector" SortExpression="tSector"
HtmlEncode="false" >
</telerik:GridBoundColumn>
And this is the code to hide the columns:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
RadGrid od = (RadGrid)RadGrid1.MasterTableView.Items[0].ChildItem.FindControl("OfferDataDetail");
Label ry = (Label)RadGrid1.MasterTableView.Items[0].ChildItem.FindControl("LblRoundYear");
int RoundYear = 2014;
if (ry != null) RoundYear = int.Parse(ry.Text);
//display for 2013
od.Columns.FindByUniqueName("LocalDeanery").Visible = (RoundYear == 2013);
od.Columns.FindByUniqueName("Sector2013").Visible = (RoundYear == 2013);
//display for 2014
od.Columns.FindByUniqueName("Region").Visible = (RoundYear == 2014);
od.Columns.FindByUniqueName("Subregion").Visible = (RoundYear == 2014);
od.Columns.FindByUniqueName("Sector2014").Visible = (RoundYear == 2014);
od.Rebind();
}
All of these columns display even when I set the display to false, a QuickWatch on the columns will give the display value as what I expect (ie true or false) but when the page loads they are all shown in the grid.
Any ideas anyone?
I've never had the problem myself, however I tend to get the columns being deleted through the MasterTableView
, rather than the column collection, therefore in your example something like the following should work:
od.MasterTableView.GetColumn("Region").Display = (RoundYear == 2014);
Personally I prefer using the Display
attribute over the Visible
attribute as this will still populate the data (in case you want to change visibility in javascript or access the text from the code behind without rebinding).
The following Telerik forum posts may also help:
http://www.telerik.com/community/forums/aspnet-ajax/grid/setting-the-property-from-codebehind.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-hide-columns-in-radgrid.aspx