Dynamic creation sfdatagrid

I would have the certitude to dynamically create a sfdatagrid starting from the string sql with the fields on which to select.
I give an example.
Strsql = "Select field1, field2, field3, field4 from Table1".
The Strsql could also be implemented with "where" to filter certain cami and "Order By" for the order.
Apart from this irrelevant detail in the overall reason, performed the Selet and created the ObsersableCollection on which supports the SfDatagrid, I should, dynamically, create the epopolarli fields using Binding.
It would be important to solve an important problem of my application.
You have some examples about it.
I am currently using access with OleDbDataReader.
Thank you
Moreno

7 Replies

GT Gnanasownthari Thirugnanam Syncfusion Team March 8, 2018 04:12 AM UTC

Hi Moreno, 

We suspect your requirement, you need to load the data from database with selected fields using OleDbDataReader at runtime in SfDataGrid. If our suspicion is different from your requirement kindly share more details about your requirement like screenshot, video or sample that will be helpful to provide appropriate solution at the earliest. 

Regards, 
Gnanasownthari T. 




MI misonsan March 9, 2018 07:38 AM UTC

Good day Gnanasownthari

First of all thanks for the attention paid to my problem.
As anticipated in the initial request, I would like to build "Dynamically" from behind code, a SfDatagrid starting from a very common sql string used to retrieve data from a table.
From the XAML point of view, Sfdatagrid is defined without fields. These should be generated run time by analyzing the list created. Then you should analyze the individual fields of the sql string and define the fields before you can populate the sfdatagrid with the actual data.
I hope I have clearly expressed the problem, but I remain obviously available for any further clarification.
In this regard, as a basis for work, I have prepared a solution with the code for the population of sfdatagrid.
Enclosed I also send you the D.B. Access used.
Thank you for your attention and I wish you a happy day.
Hello
Moreno

Attachment: SfDataGridDinamico_caf3e1bc.zip


GT Gnanasownthari Thirugnanam Syncfusion Team March 13, 2018 04:02 AM UTC

Hi Moreno, 

In your given sample you didn’t set the DataContext for Window or SfDataGrid so data does not populated in SfDataGrid. Now we have set the DataContext. 

public MainWindow() 
{ 
       InitializeComponent(); 
       this.DataContext = this; 
       mDp = new DpFornitore(AppContext.Instance.ConnString); 
 
       FornitoriList = new ObservableCollection<Fornitore>(); 
} 

Now we have modified the sample by setting DataContext, you can download it from below mentioned location. 


Please let us know if you need further assistance on this, we will happy to assist  you. 

Regards, 
Gnanasownthari T. 



MI misonsan March 13, 2018 10:39 AM UTC

Thank you Thirugnanam for the attention to my problem and for the kind reply.
I'm checking the solution you sent me, but I can hardly understand why it works and what kind of action you did to generate the list from the dynamically created data on the list table.
You pointed out to me that the problem was in the setting, in the draft I sent you for your evaluation of the problem, of the DataContext.
In my draft it is set to:
                  
DataContext = this;
while yours is set to:
                
this.DataContext = this;

I do not understand this change what can change.

The outline of your solution is escaping me.
In this regard, I try to get back to help me understand the solution you have made.

The user with this application should have the possibility to create custom lists in a dynamic way.
I give the user the possibility to select the table on which to operate.
So in an absolutely unpredictable manner, the user can select the table "customer details", the table "Orders", the table "Invoices" etc.
Once the table has been selected, the user can select the relative fields on which he wants to generate the list.
Once the selection is made, I read the selected data and "Creo" a generic table called "List" that does not define property definitions in the class entities because I can not know a priori the names and types of data.

To solve the problem in an absolutely complete manner, I should be able to generate run time, in the "List" class, the properties related to the fields selected by the user.

it's possible ?

however, you'll wonder if you could comment on your solution to make me understand how you can generate the list.

Thank you

Moreno


can you check if you can handle the problem highlighted in red?


SR Sivakumar R Syncfusion Team March 15, 2018 05:41 PM UTC

Hi Moreno, 
 
If you set SfDataGrid.AutoGenerateColumns as true, then data grid will create columns based on the ItemsSource. When you are changing ItemsSource dynamically which may have different set of columns, then set AutoGenerateColumnsMode as ResetAll, which will clear and create new columns based on newly set ItemsSource. In this way, you can generate the columns for data grid dynamically.  
 
<syf:SfDataGrid Name="Elenco" ItemsSource="{Binding FornitoriList}"                              AutoGenerateColumns="True"                                                          AutoGenerateColumnsMode="ResetAll"> 
</syf:SfDataGrid> 
 
In another way, you can manually add or remove columns based on ItemsSource using Add, Remove method of column as in the below link, 
 
I have modified the sample where initial data loaded as expected. If you change the ItemsSource at runtime also, data grid will automatically reset its columns based on new ItemsSource. 
 
Thanks, 
Sivakumar 



MI misonsan March 16, 2018 10:31 AM UTC

good morning Sivakumar
thanks for the comments on the solution you sent me.
Reflections were also effective for other points of the problem.
I still want to thank you and your team; you are fantastic professionals capable of conveying culture with grace and education.
A hug.
Moreno


GT Gnanasownthari Thirugnanam Syncfusion Team March 19, 2018 11:25 AM UTC

Hi Moreno, 

Thank you for your update, 

Please let us know if you need further assistance on this, we will happy to assist you. 

Regards, 
Gnanasownthari T. 


Loader.
Up arrow icon