Binding to IEnumerable<dynamic>

Dear Syncfusion,

I am reading your documentation about binding your Blazor grid to a dynamic /ExpandoObject. In both cases you are emphasizing the fact that the structure of these objects are not know in compile time. However in your examples you are binging the columns to fixed field names.

Let's assume, I want to show in the grid the result of the SELECT * FROM @TableName query result, where the @TableName is given to the query in runtime. Clearly the column names are not know in compile time, moreover even the number of columns can be arbitrary. Can you please guide me to a solution? What steps should I take to create such dynamic grid. 

One more important thing: the grid is read-only. There is no need to modify the data, it is only for display.

Thanks!


5 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team November 17, 2021 07:33 AM UTC

Hi Peter, 

Greetings from Synfusion support. 

We have validated your query and found that you want to use dynamic/Expando object without fixed field names. We suggest you to use the dynamic column binding feature to achieve your scenario. Please find the below code snippet, documentation and the sample for your reference. 


<SfGrid @ref="grid" 
            Height="400px" 
            Width="100%" 
            TValue="ExpandoObject" 
            DataSource=@data> 
        <GridColumns> 
            @if (data != null && data.Any()) 
            { 
                var firstItem = data.First(); 
                var dictionaryItem = (IDictionary<string, object>)firstItem; 
 
                var fields = dictionaryItem.Keys; 
 
                foreach (var item in dictionaryItem) 
                { 
                    <GridColumn Field="@item.Key"></GridColumn> 
                } 
            } 
        </GridColumns> 
    </SfGrid> 

Please get back to us if you have any other queries. 
 
Regards, 
Jeevakanth SP. 



PE Peter November 17, 2021 08:48 AM UTC

Dear  Jeevakanth,

thank you for the quick answer. Your suggestion made me a step forward: now all the fields title appears in the grid. However the grid rows are empty, they are not showing any data. It seems the number of rows are fine, but no data shown.



Attachment: Screenshot_20211117_094742_1ab6bf05.zip


JP Jeevakanth Palaniappan Syncfusion Team November 18, 2021 11:28 AM UTC

Hi Peter, 

We have checked your query but as you can see that the reported problem is not reproduced in the previously provided sample. So to proceed further we need the below details. So kindly share us the below details. 

  1. Share us the Syncfusion NuGet version details.
  2. Share us the complete grid code with your datasource.
  3. Share us the runnable issue reproducing sample or reproduce the reported problem in the below provided sample.


The above requested details will be helpful for us to validate your query based on your scenario and to provide you with a better solution as early as possible. 

Regards, 
Jeevakanth SP. 



Marked as answer

PE Peter November 18, 2021 12:36 PM UTC

Dear  Jeevakanth,


thank you very much for the full sample you provided. Based on that I was able to rewrite my app, what now works well.

Thanks again for the help!



JP Jeevakanth Palaniappan Syncfusion Team November 19, 2021 04:42 AM UTC

Hi Peter, 

We are glad that the provided solution works for you. Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon