RadGrid custom aggregate with hide/show columns

2 Answers 18 Views
Ajax Grid
QiQo
Top achievements
Rank 1
Iron
QiQo asked on 09 Oct 2025, 07:28 AM

On my pageI have a RadGrid with columns containing gross and net ammounts  and a RadButton "Gross/Net". By clicking the button the grid shows it columns accordingly. Worked liek a charm.

Then I added aggregates od type "Sum" and still everything worked fine.

After that I added an aggregate od type "Custom" to one of the columns (which calculates  and shows everything as it should) but than I lost the button funcionality, clicking it does not switch the columns visibility. 

The button does:  myGrid.MasterTableView.GetColumn("mycolumn").Display = Not mybutton.Checked.
Also tried:  myGrid.MasterTableView.GetColumn("mycolumn").Visible = Not mybutton.Checked

2 Answers, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 13 Oct 2025, 08:24 AM

Hello,

When you use a custom aggregate in a RadGrid column, the grid expects the column to be present during its lifecycle so that the aggregate calculation can be performed. If you toggle the column's visibility or display property at runtime, the grid may not properly update its internal structure, especially after a postback or rebind. This can interfere with both the custom aggregate logic and the dynamic column visibility, resulting in the button no longer working as expected.

Below you can see a simple implementation of your scenario, which causes the Grid to rebind in order to keep the proper data in tact:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="btnToggleGrossNet">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

<telerik:RadButton ID="btnToggleGrossNet" runat="server" Text="Gross/Net" OnClick="btnToggleGrossNet_Click" />

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" Width="500px" OnCustomAggregate="RadGrid1_CustomAggregate"
    ShowFooter="true" OnNeedDataSource="RadGrid1_NeedDataSource"
    OnColumnAggregate="RadGrid1_ColumnAggregate">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Item" HeaderText="Item" UniqueName="Item" />

            <telerik:GridBoundColumn DataField="GrossAmount" HeaderText="Gross Amount" UniqueName="GrossAmount"
                DataFormatString="{0:C}" Aggregate="Sum" FooterAggregateFormatString="Total Gross: {0:C}" />

            <telerik:GridBoundColumn DataField="NetAmount" HeaderText="Net Amount" UniqueName="NetAmount"
                DataFormatString="{0:C}" Aggregate="Custom" FooterAggregateFormatString="Avg Net: {0:C}" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
private bool ShowGross
{
    get { return (bool?)ViewState["ShowGross"] ?? true; }
    set { ViewState["ShowGross"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
}

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetData();
}

protected void btnToggleGrossNet_Click(object sender, EventArgs e)
{
    ShowGross = !ShowGross;
    UpdateColumnVisibility();
    RadGrid1.Rebind();
}

private void UpdateColumnVisibility()
{
    GridColumn grossCol = RadGrid1.MasterTableView.GetColumnSafe("GrossAmount");
    GridColumn netCol = RadGrid1.MasterTableView.GetColumnSafe("NetAmount");

    if (grossCol != null) grossCol.Display = ShowGross;
    if (netCol != null) netCol.Display = !ShowGross;
}

private DataTable GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Item", typeof(string));
    dt.Columns.Add("GrossAmount", typeof(decimal));
    dt.Columns.Add("NetAmount", typeof(decimal));

    dt.Rows.Add("Product A", 100m, 80m);
    dt.Rows.Add("Product B", 200m, 160m);
    dt.Rows.Add("Product C", 150m, 120m);

    return dt;
}

Try this approach and see if it will help in your case.

    Regards,
    Vasko
    Progress Telerik

    Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
    0
    QiQo
    Top achievements
    Rank 1
    Iron
    answered on 14 Oct 2025, 08:22 AM

    Vasko,
    thank you for your answer, it will  for sure help other users.

    In my case it was, ofcourse, my error.
    As on the page in question I have a RadTabStrip+RadMultiPage  with 3 RadPageViews, with "Gross/Net" buttons on two of them , ofcourrse i was looking at the wrong combination of Button-Grid.

    It works with a plain implementation of myGrid_CustomAggregate(sender As Object, e As GridCustomAggregateEventArgs)

    Tags
    Ajax Grid
    Asked by
    QiQo
    Top achievements
    Rank 1
    Iron
    Answers by
    Vasko
    Telerik team
    QiQo
    Top achievements
    Rank 1
    Iron
    Share this question
    or