Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
980
Add TemplateColumn in Code not working
posted

Hi IGTeam,

 

I am in a need to dynamically create columns for my igGrid;

I bind to data from my ViewModel, which is of type ObservableCollection<DynamicObject>;

 

Just before the binding, I want to create columns on the View side like this:

1. Create collection of TemplateColumn

2. Send to View

3. Simply add columns to grid

____________________________________________________________________

1.  Iteration in for block (populate _dynamicGridColumnKeys)

TemplateColumn column = new TemplateColumn();
            column.HeaderText = presentingName;
            column.ItemTemplate = (System.Windows.DataTemplate)System.Windows.Markup.XamlReader.Load(CreateColumnTemplate(propertyName)); //display template

 

private string CreateColumnTemplate(string propertyName)
        {
            StringBuilder CellTemp = new StringBuilder();
            CellTemp.Append("<DataTemplate ");
            CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
            CellTemp.Append("2006/xaml/presentation' ");
            CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
            CellTemp.AppendFormat("<TextBlock Text='{{Binding [{0}]}}'/>", propertyName);
            CellTemp.Append("</DataTemplate>");
            return CellTemp.ToString();
        }

2.

Messenger.Default.Send<List<TemplateColumn>>(_dynamicGridColumnKeys, Preferences.CurrentGridName);

3.

Messenger.Default.Register<List<TemplateColumn>>(this, GridName, p =>
            {
                foreach (var item in p)
                {
                    this.customGrid.Columns.Add(item);
                }
            });

 

 

Can you please tell me why this works in System.Windows.Controls.DataGrid and I can't seem to fix it with the Infragistics one?

 

Please, this is important for me, I would appreciate if you inform me about this DynamicObject issue soon

 

Thanks

Parents
  • 40030
    Offline posted

    Hi, 

    All columns int he xamGrid require their Key property to be set. If your column is bound to your data source, then you can use a TemplateColumn where the key corresponds to a property in your underlying datasource. If not, you can use an UnboundColumn and use any unique key. 

    You should be getting an exception that describes this, when you attempt to set add a column with an Invalid key or no key at all. 

    -SteveZ

Reply Children