This is a migrated thread and some comments may be shown as answers.

Hide/display RadGrid columns on pre-render

3 Answers 1335 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 15 Jan 2014, 09:20 AM
I'm trying to hide/show different columns on a nested rad grid based on a flag in the pre-render event but they are all displaying

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 my code in the pre-render
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();
}

The lines to hide/show the columns is being hit but they are still displaying, is there something else I should be doing?
thanks

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Jan 2014, 10:40 AM
Hi Andy,

From your code, if u set Items[0], it is hiding the Column only for the first row of the nested Radgrid. If you want to hide in such a way try as follows:

 C#:

od.Columns.FindByUniqueName("Region").Visible = false;
//OR
od.MasterTableView.GetColumn("Region").Visible = false;

In case if you are trying to hide a column in the nested view grid for all the rows please set HierarchyLoadMode="ServerBind" and try:
C#:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
 {
   GridDataItem parentItem = e.Item as GridDataItem;
   RadGrid grid = parentItem.ChildItem.FindControl("OfferDataDetail") as RadGrid;
   grid.MasterTableView.GetColumnSafe("Region").Visible = false;
   grid.Rebind();
 }
}

Thanks,
Princy
0
Andy
Top achievements
Rank 1
answered on 15 Jan 2014, 11:05 AM
I had read it's better to hide columns in the pre-render event, is that the case? If so, how would the code look then?

thanks
0
Princy
Top achievements
Rank 2
answered on 16 Jan 2014, 04:21 AM
Hi Andy,

In order to hide the columns inside the nested radgrid, you can call the nested gird's PreRender event and hide the columns in it as follows:

ASPX:
<NestedViewTemplate>
 <telerik:RadGrid ID="RadGrid2" runat="server" OnPreRender="RadGrid2_PreRender". . >
    <MasterTableView>
        <Columns>
        . . . . . .
        </Columns>
    </MasterTableView>
 </telerik:RadGrid>
</NestedViewTemplate>

C#:
protected void RadGrid2_PreRender(object sender, EventArgs e)
{
 RadGrid radgrid2 = (RadGrid)sender;
 radgrid2.MasterTableView.GetColumnSafe("Region").Visible = false
}

Thanks,
Princy
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or