GetColumnsAsync() returns nested results when using stacked columns - is there a method to return a flat list?

Hi,

GetColumnFieldNamesAsync(), GetColumnsAsync() and other methods return the list of columns as though the stacked columns are part of the column list and the columns inside those stacks are instead nested inside the list elements relating to the stacked column they are inside of.

So if I wanted to match a column with a cell returned by something like GetSelectedRowCellIndexesAsync(), which returns cell indices ignoring the stacked columns, I would have to search both the columns and nested columns returned by GetColumnFieldNamesAsync() to get an answer.


Is there an easier way to simply return the list of columns in a flat list, ignoring whether the columns are inside a stack or not?


Thanks.


Here is an example grid. with two levels of stacking.

Image_3849_1751852178595


3 Replies

GR Guhanathan Ramanathan Syncfusion Team July 7, 2025 02:00 PM UTC

Hi ,


Greetings from Syncfusion,

 

When a cell is selected in the Syncfusion Blazor Grid, the code retrieves the selected cell’s column index and uses GetColumnsAsync() to obtain the list of columns. Since the grid uses stacked (nested) columns, this method returns a hierarchical structure. The code then compares the selected column’s header text with the nested columns to identify the parent (stacked) column it belongs to. This approach helps map the flat cell index (from the selection) to the correct nested column and its parent, as the selection index does not account for the stacked structure. This is the possible ways to retrieve column details when stacked headers are used. Please refer to the sample below.


     <SfGrid @ref="GridRef" DataSource="@Orders" AllowSelection="true" >
    <GridEvents CellSelected="CellSelectedHandler" TValue="Order"></GridEvents>
    <GridSelectionSettings Mode="SelectionMode.Cell"></GridSelectionSettings>
    <GridColumns>
        <GridColumn HeaderText="Order Details">
            <GridColumns>
                <GridColumn Field="OrderID" HeaderText="Order ID" Width="120"></GridColumn>
                <GridColumn Field="CustomerID" HeaderText="Customer ID" Width="120"></GridColumn>
            </GridColumns>
        </GridColumn>
        <GridColumn HeaderText="Shipping">
            <GridColumns>
                <GridColumn Field="ShipCity" HeaderText="Ship City" Width="120"></GridColumn>
                <GridColumn Field="ShipCountry" HeaderText="Ship Country" Width="120"></GridColumn>
            </GridColumns>
        </GridColumn>
    </GridColumns>
</SfGrid>



<SfButton OnClick="GetSelectedCellsInfo">Get Selected Cell Column Fields</SfButton>



@code {
    private SfGrid<Order> GridRef;
    private List<Order> Orders = new List<Order>
    {
        new Order { OrderID = 1001, CustomerID = "ALFKI", ShipCity = "Berlin", ShipCountry = "Germany" },
        new Order { OrderID = 1002, CustomerID = "ANATR", ShipCity = "Madrid", ShipCountry = "Spain" }
    };
    public class Order
    {
        public int OrderID { get; set; }
        public string CustomerID { get; set; }
        public string ShipCity { get; set; }
        public string ShipCountry { get; set; }
    }
    public void CellSelectedHandler(CellSelectEventArgs<Order> args)
    {
        var num1 = GridRef.GetSelectedRowCellIndexesAsync().Result;
        var num2 = num1[0].Item2;
        var num3 = GridRef.GetColumnsAsync().Result;
        Console.WriteLine(num3[num2].HeaderText);
        foreach(GridColumn col in GridRef.Columns){
            if(col.Columns != null  ){
                foreach (GridColumn cold in col.Columns){
                    if (cold.HeaderText == num3[num2].HeaderText)
                    {
                        Console.WriteLine(col.HeaderText);
                    }

                }
            }

        }

    }
}



please get back to us with more details if you are facing any troublue or issue with above code

 

Regards,

Guhanathan R




Attachment: BlazorApp3_2d30f802.zip


PA Paul July 7, 2025 09:39 PM UTC

Thanks for the quick reply.

Yes, I could indeed loop through the (nested) columns collections until I find a match.  I was wondering though if you had an alternative flat list method.

I'll just loop through then.


Thanks

Paul



GR Guhanathan Ramanathan Syncfusion Team July 8, 2025 12:50 PM UTC

Hi Paul,

 

Thanks for the Update,

 

We would like to inform that currently we don’t have any method support to return only the inner level of column when multi level column is defined in Grid. So we request you to achieve your requirement using GetColumnsAsync method provided in previous update.

Please get back to us if you have further queries.

 

Regards,

Guhanathan R


Loader.
Up arrow icon