Programmatically Update RowDetailsTemplate?

May 7, 2009 at 8:16 PM
Hi there.

I'm trying to add some columns to a data grid that is defined in the RowDetailsTemplate of an outer grid.

I see there is a method on the dataGrid.RowDetailsTemplate.LoadContent  that will return the nested dataGrid.  I can get ahold of the nested grid and programmatically, .NET allows me to add new columns however, when displayed, the new columns don't show.

When this loop is completed, I can see the new columns added to the datagrid.....   what steps do I need to take to update the datagrid defn in the RowDetailsTemplate?


Here is a snippet:
 //get the datagrid from the RowDetailsTemplate   which is the nested Grid
                Microsoft.Windows.Controls.DataGrid dataGridOrders = dataGridCutomers.RowDetailsTemplate.LoadContent() as DataGrid;
                if (dataGridOrders != null)
                {
                    #region dynamically create the columns that need to bind to  child attribute
                    //use the first element as a template to create the columns
                    //the Assumption is that there will always be the same number of columns for any grid 
                    //or nested grid (though the parent grid and child grid may not need the same #)
                    //
                    for (int count = 0; count < Model[0].Orders[0].OrderDetails.Count; count++)
                    {

                        DataGridTextColumn newColumnX = new DataGridTextColumn();
                        //The header can come from somewhere else...this is for example purposes

                        newColumnX.Header = "OrderDetails.Quantity " + count.ToString();
                        Binding bindingX = new Binding("OrderDetails[" + count.ToString() + "].Quantity");
                        bindingX.Mode = System.Windows.Data.BindingMode.TwoWay;
                        newColumnX.Binding = bindingX;
                        dataGridOrders.Columns.Add(newColumnX);
                    }
                    #endregion
                }


Thanks!
May 28, 2009 at 9:08 PM

To update everyone:

To make this work... we can't use the dataGridCutomers.RowDetailsTemplate.LoadContent().  It only provides is a copy of the template and doesn't impact the actual RowDetailsTemplate.

Instead, I have an event delegate to the grid's LoadingRowDetails event and moved the code above,  to that event.

Thanks for the pointer Samantha!

Tracy