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

Programmatically create Grid / Columns?

3 Answers 387 Views
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Veteran
Jonathan asked on 28 Mar 2020, 11:55 AM

Hi

I want to bind a grid to a source source. But the data source varies each time. 

1. Can you automatically generate the columns when binding?

2. Can you programatically create a grid ?

thx in advance

 

 

 

3 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 28 Mar 2020, 03:23 PM

Hello Jonathan,

The feature that would let the grid generate columns based on the data source is this one, so you can click the Follow button to get status updates for it: https://feedback.telerik.com/blazor/1418456-bind-to-datatable. I have added your Vote for it to raise its priority.

For the time being, I'd suggest you implement a column chooser or at least the loop over the column descriptor from this feature request: https://feedback.telerik.com/blazor/1450105-column-chooser. Perhaps you could create descriptors and populate their collection based off your data, maybe through reflection.

On programmatic creation - the component model in Blazor is not designed for this. The way to add components conditionally is to use razor syntax in your views - if-blocks, foreach loops and so on to declare the grid instances as needed so they render based on your conditions and data. This applies to our grid and to any other components. There are ways to hack through that by creating a RenderFragment yourself, but I personally find that extremely cumbersome and unmaintainable.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Jonathan
Top achievements
Rank 1
Veteran
answered on 29 Mar 2020, 01:21 PM

thx..!

I was also able to use an ExpandoObject - populate with that from whatever is returned from a query

 

            <TelerikGrid Data="@expandoList"  Pageable="true" Resizable="true" Reorderable="true" Groupable="false">
              
                <GridColumns>
                    @foreach (ExpandoObject exo in expandoList)
                    {
                        @foreach (var property in (IDictionary<String, Object>)exo)
                        {
                            Console.WriteLine(property.Key + ": " + property.Value);

                            <GridColumn Field="@property.Value.ToString()" Title="@property.Key">
                                <Template>
                                    @{

                                        string toRender = "";
                                        toRender = property.Value.ToString();
                                        @toRender 
                                    }
                                </Template>
                            </GridColumn>         
                         }
                    }
                </GridColumns>

0
Marin Bratanov
Telerik team
answered on 30 Mar 2020, 08:54 AM

Hi Jonathan,

You may also want to Follow this enhancement (I added your Vote on your behalf): https://feedback.telerik.com/blazor/1429858-please-add-support-for-binding-grids-to-dynamic-expandoobject.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Veteran
Answers by
Marin Bratanov
Telerik team
Jonathan
Top achievements
Rank 1
Veteran
Share this question
or