我试图在预渲染事件中隐藏/显示嵌套telerik rad网格上的不同列,基于标志,但无论我将可见或显示属性设置为
,它们都会显示这些是我的网格列:
<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>
这是隐藏列的代码:
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();
}
即使我将显示设置为false,所有这些列都会显示,列上的QuickWatch会将显示值显示为我期望的值(即true或false),但是当页面加载时,它们都显示在网格中。
任何想法?
答案 0 :(得分:8)
我自己从未遇到过这个问题,但是我倾向于通过MasterTableView
而不是列集合删除列,因此在您的示例中,类似下面的内容应该有效:
od.MasterTableView.GetColumn("Region").Display = (RoundYear == 2014);
我个人更喜欢在Display
属性上使用Visible
属性,因为这仍会填充数据(如果您想更改javascript中的可见性或从后面的代码访问文本而不重新绑定)
以下Telerik论坛帖子也可能有所帮助:
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