I am creating columns programmatically in the GridView control. As I need to bind them afterwards, I need access to the DataMemberBinding property, so I cannot add columns of type GridViewColumn to the columns collection and I therefore directly add columns of type GridViewDataColumn to the columns collection.
The problem when doing so is that the GridView control seems to have skipped some initialization code. I may be mistaking in my assumptions, but the GridView control does clearly not like that beause lots of things do not work anymore or crash. Sorting for example crashes at the second sort, sort indicators do not appear, column reordering does not work, column grouping works for all columns even if I set it to false for a specific column, setting a specific column Readonly property causes crashes when you attempt to edit a cell, the TextAlignement has no effect, etc... really lots of behaviors crash or do not work.
Is there a way to manipulate the grid programmatically and have a normal behavior ? Here is an extract of my code:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DataGridObject = this.GetTemplateChild(DATAGRID_NAME) as RadGridView;
this.AttachEventsHandlers();
this.SetDeferredProperties();
this.CreateColumns();
this.HideGroupPanelIfUnnecessary();
this.ApplySorting();
}
private void CreateColumns()
{
List<GridViewColumn> columns = new List<GridViewColumn>(this.Columns.Count);
if (!this.IsDataGridObjectValid)
{
return;
}
this.DataGridObject.Columns.Clear();
foreach (DataGridColumn column in this.Columns)
{
columns.Add(CreateColumn(column));
}
this.DataGridObject.Columns.AddRange(columns);
}
private static GridViewColumn CreateColumn(DataGridColumn column)
{
if (column == null)
{
return null;
}
// We don't set the SortingState here via the property because it has no effect
// (but a little sort indicator (arrow) is nevertheless anyway drawn in the header cell)
return new GridViewDataColumn
{
HeaderText = column.HeaderText,
DataMemberBinding = column.DataMemberBinding,
Width = column.Width,
UniqueName = column.DataMemberBinding.Path.Path,
};
//The following properties currently do not work or cause crashes
//IsReadOnly = column.ReadOnly,
//IsSortable = column.AllowSorting,
//IsGroupable = column.AllowGrouping,
//TextAlignment = column.TextAlignment,
//Background = column.Background,
}
The exact situation is that I have a class called DataGrid, which has a ControlTemplate defined in themes\generic.xaml. This control is intended to be part of an UI framework because we do not want references to third-party controls in our applications. This DataGrid control therefore exposes among others columns, and it is intended to be used in the Xaml of our applications. When it gets initialized, it looks at its configuration, its own columns collection for example, and it sets the properties of its RadGridView control programmatically accordingly to its own settings, so we really cannot let the GridView control auto-generate its columns or define columns the standard way in Xaml because the client applications have no direct references to Telerik libraries.
We are currently using the trial version till the purchase order has been processed. The version of Telerik.Windows.Controls.GridView.dll is 2008.3.1217.1020.
Thanks in advance !
Jean-Christophe