We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Dynamic columns creation through data binding

Hi,
I am wondering if there a way to dynamically create columns using data binding. Lets says I have a class like below:

Student
{
int ID, 
string Name
List<Subject> SubjectList
)

Columns: ID, Name will be statically bound and I like to create one column per Subject in SubjectList collection with subject name as header text and subject.Score will be its value.  

9 Replies

JG Jai Ganesh S Syncfusion Team April 13, 2015 01:58 PM UTC

Hi Vasanth,

Thank you for using Syncfusion products.

We have analyzed your query. You can achieve your requirement for dynamically adding the columns and set the SubjectName as HeaderText and scores as values by using Indexer binding. We have prepared a sample based on this and please find the sample under the following location,

Sample: http://www.syncfusion.com/downloads/support/directtrac/137656/SfGridComplexPropertySample-1400735603.zip

Please let us know if you need further assistance.

Thank you,

Jai Ganesh S



VA Vasanth April 13, 2015 02:09 PM UTC

Thanks for prompt response Jai Ganesh! I looked at the sample and  dynamic columns are being achieved in code behind. I am wondering if there a way I can do that in xaml? Also the Subjects collection is a variable so I would want the XAML binding to create those columns dynamically as well.


JG Jai Ganesh S Syncfusion Team April 14, 2015 09:33 AM UTC

Hi Vasanth,

 

Thank you for the update.

 

We have analyzed your query. You can also create the columns by using  indexer binding in Xaml like code behind. We have modified the sample based on this and please find the sample under the following location,

 

Sample:

http://www.syncfusion.com/downloads/support/directtrac/137224/SfGridComplexPropertySample-363928122.zip

 

Please let us know if you need further assistance.

 

Thank you,

Jai Ganesh S



VA Vasanth April 14, 2015 01:45 PM UTC

Hi Jai Ganesh,
Thanks for the sample! Binding in XAML is still being acheived in static way. You are currently adding four hard coded columns with subject indexer from 0-3. But the number of subjects can vary so I am wondering if I can dynamically create columns in XAML based on variable number of subjects.
 
Vasanth


JG Jai Ganesh S Syncfusion Team April 15, 2015 10:43 AM UTC

Hi Vasanth,


We areunclearabout how SfDataGrid generatescolumns for the indexer properties. If you are expecting something like similar to SfDataGrid.AutoGenerateColmns property, thenit is not possible to auto generate columns for complex/indexer properties. If you want to add columns dynamically,then you have to use code behind to add columns based onyour requirement.


We have support to auto generate columns of direct properties of data by setting SfDataGrid.AutoGenerateColumns. But, you have to define column in XAML or you have to add columns from code behindfor complex and indexer property.


Please let us know if you need further assistance.


Thank you,

Jai Ganesh S




VA Vasanth April 15, 2015 01:42 PM UTC

Hi Jai Ganesh,

Yeah I was hoping it will be possible to dynamically create columns for indexer properties in XAML. I really appreciate your help  & effort. thanks!


SC Saranya CJ Syncfusion Team April 16, 2015 08:51 AM UTC

Hi Vasanth,

Thank you for your update. Please let us know if you require further assistance on this.

Regards,
Saranya


SA Sreeram Akella June 15, 2015 04:11 PM UTC

Hi, 

I have a very similar requirement in one of my projects and so I used your sample. However, I found that the DataGrid crashes while trying to sort the generated columns. In the sample, when I tried to sort "Physics" column, the grid crashes with an exception message, 'SubjectList[0]' is not a member of type 'SfGridComplexPropertySample.Student'. 

Is there a different way to achieve dynamic columns? I remember solving a similar problem in WPF using ICustomTypeDescriptor implementation (Much cleaner and testable approach)Does SfDataGrid support this interface or anything similar?

Thanks,
Ram.


JG Jai Ganesh S Syncfusion Team June 16, 2015 09:31 AM UTC

Hi Vasanth,

We are sorry about the inconvenience caused.

We analyzed your query. You can achieve your requirement by setting “UseBindingValue” property as true in the column. When we set UseBindingValue property in GridColumn to true then Sorting, Grouping and Filtering make use of binding defined in GridColumn instead of reflection to manipulate the data.

Code Snippet [XAML]:

<syncfusion:GridTextColumn MappingName="SubjectList[0].Score" HeaderText="Physics" UseBindingValue="True"/>


We have also modified the sample based on this and please find the sample under the following location:

Sample: http://www.syncfusion.com/downloads/support/directtrac/126438/ze/SfGridComplexPropertySample2100089253

Please find the following Kb link to shows the UseBindingValue in Complex properties,

Kb Link:

http://help.syncfusion.com/ug/wpf/index.html#!Documents/complexpropertybinding.htm

Please let us know if you need further assistance.

Thank you,

Jai Ganesh S


Loader.
Live Chat Icon For mobile
Up arrow icon