datagrid with 2 header rows and variable content

I'm using Blazor server

I don't find the right way, if possible, to do the following:

1) Writing in the datagrid column header variable values

2) Adding to the datagrid a second header row as showed in the picture below




12 Replies 1 reply marked as answer

MS Monisha Saravanan Syncfusion Team June 8, 2022 11:33 AM UTC

Hi Walter,


We have checked your query and we suspect that you need to add multiple header to the DataGrid. We would like to inform that in Grid we have support to render stacked header. We have already documented this topic kindly refer the attached UG and demo for your reference.


Reference: https://blazor.syncfusion.com/documentation/datagrid/columns#stacked-column-header

https://blazor.syncfusion.com/demos/datagrid/stacked-header?theme=bootstrap4


Kindly get back to us if you face any difficulties or if you have further queries.


Regards,

Monisha



WM Walter Martin June 8, 2022 12:50 PM UTC

Actually the stacked header is not exactly what I need but I could use your suggestion if there's a way to write in the HeaderText property of every column a variable content

something like:

<GridColumns>

        <GridColumn Field=ArticoloId HeaderText=@{variable} AutoFit=true></GridColumn>

</GridColumns>

  

is this possible ? 

The variable value belongs to the data in that specific column so I could get it from database or maybe there's a way to set it in the grid data bound ?set in 



MS Monisha Saravanan Syncfusion Team June 9, 2022 12:02 PM UTC

Hi Walter,


Thanks for the update.


We suspect that you need to customize the Grid header. So we suggest you to use Grid header template feature of DataGrid and you can customize the header as per your requirement. Kindly refer the attached UG for your reference.


Reference: https://blazor.syncfusion.com/documentation/datagrid/columns#header-template

https://blazor.syncfusion.com/documentation/datagrid/columns#dynamic-column-building


Kindly get back to us if you have further queries.


Regards,

Monisha



WM Walter Martin June 10, 2022 12:08 PM UTC

thanks for your reply, 

I totally changed the way to build the grid and now, using the wonderful ExpandoObject databinding, I solved several problems.


Now I'm trying to use Aggregates with ExpandoObject but the code below doesn't work

The piece of code in bold seems to be totally ignored and the grid has no aggergates line on footer

Even if I delete the   if (prop == "Totpezzi") condition the aggretes are not showed


what could be the source of problem ?



<SfGrid ID="GridBolle" DataSource="@CustomerList" TValue="ExpandoObject" AllowPaging="true" AllowPdfExport="true" AllowExcelExport="true" AllowFiltering="true" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport", "PdfExport" })">

    <GridPageSettings PageSize="10" PageSizes="true"></GridPageSettings>

    <GridEvents OnToolbarClick="ToolbarClick" TValue="ExpandoObject" />

    <GridColumns>

        @{

            foreach (var val in Columns)

            {

                <GridColumn Field="@val" IsPrimaryKey="@(val == "ArticoloId")"></GridColumn>

            }

        }

    </GridColumns>

    <GridAggregates>

        <GridAggregate>

            <GridAggregateColumns>

                @{

                    foreach (var prop in Columns)

                    {

                            <GridAggregateColumn Field=@prop Type="AggregateType.Sum">

                                <FooterTemplate>

                                @{

                                    if (prop == "Totpezzi")

                                    {

                                        var aggregate = (context as AggregateTemplateContext);

                                        <div>

                                            <p>Sum: @aggregate.Sum</p>

                                        </div>

                                    }

                                }

                                </FooterTemplate>

                            </GridAggregateColumn>

                   }

                }

            </GridAggregateColumns>

        </GridAggregate>

    </GridAggregates>

</SfGrid>




MS Monisha Saravanan Syncfusion Team June 13, 2022 11:22 AM UTC

Hi Walter,


Thanks for the update.


We have checked your query and we suspect that you are facing issue with aggregate column when using it with dynamic column building. We have prepared an sample based on your shared code snippet. Kindly check the attached sample and code snippet for your reference.



 

<SfGrid DataSource="@OrderData" @ref="Grid">

    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings>

    <GridAggregates>

 

        <GridAggregate>

 

            <GridAggregateColumns>

 

                @{

 

                    foreach (var prop in Grid.Columns)

 

                    {

                            @if (prop.Field == "EmployeeID")

                        {

 

                                    <GridAggregateColumn Field[email protected] Type="AggregateType.Sum">

 

                                        <FooterTemplate>

 

                                        @{

                                     

                                        var aggregate = (context as AggregateTemplateContext);

 

                                                        <div>

 

                                                            <p>Sum: @aggregate.Sum</p>

 

                                                        </div>

 

                                        }

 

                                        </FooterTemplate>

 

                                    </GridAggregateColumn>

 

                   }

 

                }

                }

 

            </GridAggregateColumns>

 

        </GridAggregate>

 

    </GridAggregates>

</SfGrid>

 


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp1932452541.zip


If the reported issue still reproduced then kindly get back to us with an issue reproduceable sample or try to modify the above mentioned sample and kindly get back to us if you have further queries.


Regards,

Monisha



WM Walter Martin June 13, 2022 03:35 PM UTC

I don't know if this is relevant but I'm using ExpandoObject and my piece of code below doesn't show the aggregates

The number of columns is variable 

I added a breakpoint but even if the "@if (prop.Field == "Totpezzi")" is true, the piece of code inside this if condition is not performed surprisingly

All the right columns values are always showed but not the line with the sum value.


<SfGrid ID="GridBolle" @ref="grbolle" DataSource="@BolleList" TValue="ExpandoObject" AllowPaging="true" AllowPdfExport="true" AllowExcelExport="true" AllowFiltering="true" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport", "PdfExport" })">

    <GridPageSettings PageSize="10" PageSizes="true"></GridPageSettings>

    <GridEvents OnToolbarClick="ToolbarClick" TValue="ExpandoObject" />

        <GridAggregates>

        <GridAggregate>

            <GridAggregateColumns>

                @{

                    foreach (var prop in grbolle.Columns)

                    {

                       @if (prop.Field == "Totpezzi")

                        {

                            <GridAggregateColumn [email protected] Type="AggregateType.Sum">

                                <FooterTemplate>

                                @{

                                        var aggregate = (context as AggregateTemplateContext);

                                        <div>

                                            <p>Sum: @aggregate.Sum</p>

                                        </div>

                                }

                                </FooterTemplate>

                            </GridAggregateColumn>

                        }

                    }

                }

            </GridAggregateColumns>

        </GridAggregate>

    </GridAggregates>

    <GridColumns>

        @{

            foreach (var val in Columns)

            {

                <GridColumn Field="@val" IsPrimaryKey="@(val == "ArticoloId")"></GridColumn>

            }

        }

    </GridColumns>


</SfGrid>



WM Walter Martin June 13, 2022 10:31 PM UTC

I found the source of problem but not a good solution

The sum value appeared after clicking in the column header to sort the data so it seems to be it's necessary to make a sort of grid refresh because probably the columns are created too early

How can I force a grid sorting before rendering it ?




MS Monisha Saravanan Syncfusion Team June 14, 2022 01:35 PM UTC

Hi Walter,


Thanks for the update.


We have checked your checked your query we suspect that the reported issue occurs due to time delay.

Kindly try the below solution at your end. We have attached sample for your reference. Kindly refer the attached sample


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp1-132488962.zip


<GridAggregate>

 

            <GridAggregateColumns>

 

                @{

                var firstItem = Orders.First();

                var dictionaryItem = (IDictionary<string, object>)firstItem;

 

                    foreach (var prop in dictionaryItem)

 

                    {

                            @if (prop.Key == "EmployeeID")

                        {

 

                                    <GridAggregateColumn Field[email protected] Type="AggregateType.Sum"></GridAggregateColumn>

 

                   }

                  }

                }

 

            </GridAggregateColumns>

 

        </GridAggregate>

 

    </GridAggregates>


Also we would like to inform that we have support to perform initial sorting. We have already documented this topic kindly check the attached UG for your reference.


Reference: https://blazor.syncfusion.com/documentation/datagrid/sorting#initial-sort


If you still face any difficulties then kindly get back to us with an issue reproduceable sample or try to reproduce the issue on the above mentioned sample. So that it would be helpful for us to validate the issue further at our end and help us to provide solution ASAP.


Regards,

Monisha



WM Walter Martin June 14, 2022 03:59 PM UTC

I changed in attachment your sample to make it similar to mine and as you can see, I fill the grid datasource not from the beginning but after selecting something in the dropdownlist

In this case the sum value is showed only after clciking in the column header to filter the data for instance.

How can I show the sum value just after selecting a value in the dropdownlist ?



Attachment: BlazorApp1_43288a5f.zip


MS Monisha Saravanan Syncfusion Team June 15, 2022 01:02 PM UTC

Hi Walter,


Thanks for the update.


We are able to reproduce the reported query at the provided sample and we could see that before columns refreshed we are trying to calculate Grid Aggregates. So the reported issue reproduced. So we have called RefreshColumnsAsync method of DataGrid before calling Refresh method to refresh the columns. Kindly refer the below code snippet and attached sample for your reference.


 

<SfGrid DataSource="@Orders" @ref="Grid" AllowSorting=true>

 

</SfGrid>

 

@code{

 

    public async Task ChangeTipologia(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, Tipologia> args)

    {

      

        if (args.ItemData != null)

        {

            Orders = Enumerable.Range(1, 10).Select((x) =>

            {

                dynamic d = new ExpandoObject();

                d.OrderID = 1000 + x;

                d.CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)];

                d.EmployeeID = (new double[] { 2, 1, 4, 5, 3 })[new Random().Next(5)] * x;

                return d;

            }).Cast<ExpandoObject>().ToList<ExpandoObject>();

 

        }

  

        await Grid.RefreshColumnsAsync();

        await Task.Delay(300);

        await Grid.Refresh();

 

    }

  

}


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp1_43288a5f815894619.zip


Kindly get back to us if you have further queries.


Regards,

Monisha


Marked as answer

WM Walter Martin June 15, 2022 10:53 PM UTC

this is what I needed 

thanks a lot



MS Monisha Saravanan Syncfusion Team June 16, 2022 04:27 AM UTC

Hi Walter,


Most welcome. Kindly get back to us if you have further queries. As always we will be happy to help you.


Regards,

Monisha


Loader.
Up arrow icon