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

Dynamically adding and binding columns

Hello,

I was wondering if it was possible, and if so how, to add "dynamically" columns to a wpf data grid in syncfusion.

Say I have a hypothetical class that has a few properties:

public class Player
{
public string Name { get; set; }
public string Position { get; set; }

public List PointsPerVenue { get; set; }
}

where PointsPerVenue would be a list an unknown size that lists the points a certain player scores in a certain venue.

now in my ViewModel I would have an ObservableCollection that I would probably bind my datagrid's itemsource to.

I would also have a List property that would list the venues the group of player showed in my datagrid played in.

so...

public class ViewModel1
{
public ObservableCollection PlayerGroup {get;set;}
public List Venues { get; set; }
}

Can I somehow dynamically add the columns where the venues list would be the column header's and the cells would be bound to the appropriate member of the Player's PointsPerVenue list?

Thanks!



12 Replies

SN Sankara Narayanan N Syncfusion Team April 9, 2012 07:17 PM UTC

Hi Oaha,

Thanks for using Syncfusion products.

we have prepared a sample as per your query and please find the sample from the attachment.

In the sample, we have achieved your requirement by binding the Venus as like complex property binding and it is not possible to bind a collection which a collection with it. if you want to achieve your requirement, you can have separate collection and that can be bound to a column or else you can use nested grid.

Please let us know if you have queries.

Thanks,
Sankar



92920-1093393862_f0ced6d7.zip


KG Keller Grimmitt January 31, 2013 06:12 PM UTC

Is there any way to do this in the xaml code?  I'd like to bind to a list and have syncfusion grid create a column for each object in the list.  


RG Rajasekar G Syncfusion Team February 5, 2013 04:33 AM UTC

Hi Keller, 

Thank you for your update.

We have analyzed your query and we have achieve your requirement by using the following code snippet. 

Code Snippet[Xaml]:

<syncfusion:GridDataControl 

                        x:Name="DataGrid" 

                        ItemsSource="{Binding GDCSource}" 

                        ShowAddNewRow="False"  

                        AutoPopulateColumns="False" 

                        local:GDCAttachedProperty.DynamicColumns="{Binding VisibleColumns}">         

</syncfusion:GridDataControl>

 

We have prepared a sample based on this and it can be download from the following location. 

Sample Location: GridDataControl_VisibleColumnsBinding.zip 

Please let us know if you have any questions. 

Regards,

Raja sekar.G



RV Rajiv Verma September 11, 2013 10:12 AM UTC

The XAML binding solution zip file link is broken. Please provide the correct link.


DB Darren Baines September 17, 2013 02:30 PM UTC

I also need a copy of this file. Please fix the broken link.


GA Gobinath A Syncfusion Team September 19, 2013 08:34 AM UTC

Hi Darren,

We apologies for  the delay caused.

The provided sample was unable to download at you end might be because of our server issue. We have reloaded the sample again and could you please check now.

Sample: GridDataControl_VisibleColumnsBinding.zip

Please let us know if you have any queries.

Regards,

Gobinath A




KO koanbock April 10, 2016 06:44 PM UTC

Can you reupload the sample? I would need this.

Thanks!


JS Jayapradha S Syncfusion Team April 12, 2016 09:34 AM UTC

Hi koanbock,

Please find the sample from the following location,
Sample Link: http://www.syncfusion.com/downloads/support/forum/103058/ze/GridDataControl_VisibleColumnsBinding83567388

Regards,
Jayapradha


KO koanbock April 17, 2016 08:17 PM UTC

Thanks for hte reupload!

I looked into it and have a short question about the binding.

Can I bind a Column to an Arrayitem?

Something like 

 for (var i = 1; i <= length; i++)
{
   list.Add(new GridDataVisibleColumn() { HeaderText = i.ToString(), MappingName = $"ColumnArray[{i}].Text" });
}

Thanks!


SV Srinivasan Vasu Syncfusion Team April 18, 2016 12:06 PM UTC

Hi koanbock,

We have prepared a sample as per your requirement. In this sample, we have created dynamic VisibleColumns using Indexer Property binding.
Please refer the below code example
C#

private List<GridDataVisibleColumn> Getvisiblecolumn()
        {
            List<GridDataVisibleColumn> list = new List<GridDataVisibleColumn>();       

            for (int i = 0; i < 6; i++)
            {
                var bind  = new System.Windows.Data.Binding()
                {
                    Path =new System.Windows.PropertyPath($"ColumnArray[{i}].Text")
                };

                list.Add(new GridDataVisibleColumn()   // DynamicColumns
                {
                    MappingName = $"ColumnArray[{i}].Text",
                    Binding = bind,
                    //Must set this property to let the GridDataControl know the type of property, you are binding when using arrarys
                    VisibleColumnType = typeof(string)
                });
            }
            return list;
        }

        private ObservableCollection<Data> Getsource()
        {           
          ObservableCollection<Data> items = new ObservableCollection<Data>();

            if (items != null)
            {
                for (int i = 0; i < 6; i++)
                {
                    Data data = new Data();
               
                    for (int j = 0; j < 6; j++)
                    {
                        data.ColumnArray[j] = new Model();
                        data.ColumnArray[j].Text = "customer" + i;
                    }
                    items.Add(data);
                }
            }
            return items;
        }


Please find the sample from the below location:
http://www.syncfusion.com/downloads/support/forum/103058/ze/GridDataControl_VisibleColumnsBinding_-_Array-1073497387

Please let us know if you have queries.
Regards,
Srinivasan



KO koanbock April 18, 2016 05:14 PM UTC

Hello,

thanks for the sample working like a charm creating the columns but when I change the Row 1 Column 1 Text and move to the next row it gets the same value. Seems like the control doesnt update properly.

Regards,
Koanbock


SV Srinivasan Vasu Syncfusion Team April 20, 2016 01:28 AM UTC

Hi koanbock,

In GridDataControl, while binding columns dynamically the same value updated in all rows of same column when editing the data. This is actual behavior of  GridDataControl. So, we have recommended you to use only SfDataGrid in your application and currently we have implemented many features in SfDataGrid. SfDataGrid has been designed based on the WPF template-based architecture which provides support to customize the Grid easily. Comparatively, the performance of SfDataGrid control is better than the GridDataControl.

UG Link: http://help.syncfusion.com/wpf/sfdatagrid/overview


Please let us know if you have any concerns.


Regards,
Srinivasan




Loader.
Live Chat Icon For mobile
Up arrow icon