I am creating a collection of columns in the viewmodel and successfully adding the columns to the TreeListView from the code behind in an event in the viewmodel.
CodeBehind
private void Vm_OnCompletedEvent(object sender, EventArgs e) { try { tvData.Columns.Clear(); foreach (GridViewDataColumn oCol in vm.ColumnList) { this.tvData.Columns.Add(oCol); } tvData.Rebind(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }The XAML in the View
<telerik:RadTreeListView x:Name="tvData" Margin="1" ItemsSource="{Binding TreeNodeDEList}" SelectedItem="{Binding SelectedTreeNodeDE,Mode=TwoWay}" AutoGenerateColumns="False" AutoExpandItems="True" IsFilteringAllowed="False" IsReadOnly="True" HierarchyIndent="8" FrozenColumnCount="1" RowStyleSelector="{StaticResource deRowSelector}" RowIndicatorVisibility="Collapsed"> <telerik:RadTreeListView.ChildTableDefinitions> <telerik:TreeListViewTableDefinition ItemsSource="{Binding ChildNodes}" /> </telerik:RadTreeListView.ChildTableDefinitions> </telerik:RadTreeListView>The ViewModel code to create the columns
private void CreateColumns(){ try { string sUniqueName, sValuefield; GridViewDataColumn oCol; ColumnList = new List<GridViewDataColumn>(); oCol = new GridViewDataColumn(); oCol.HeaderCellStyle = HeaderStyle(); oCol.Width = 250; oCol.Header = "Category/Line Item"; oCol.UniqueName = "Rootcol"; oCol.DataMemberBinding = new System.Windows.Data.Binding("NodeLabel"); oCol.HeaderTextAlignment = TextAlignment.Left; oCol.TextAlignment = TextAlignment.Left; ColumnList.Add(oCol); int i = 1; foreach (WeekNoDB oWk in lWeekNo) { sUniqueName = string.Format("CellLabel{0}", i.ToString().PadLeft(2, '0')); sValuefield = string.Format("CellLabel{0}", i.ToString().PadLeft(2, '0')); oCol = new GridViewDataColumn(); oCol.HeaderCellStyle = HeaderStyle(); oCol.Width = 40; oCol.Header = string.Format("{0}/{1}", oWk.ToDate.DateTimeFromNull().Day, oWk.ToDate.DateTimeFromNull().Month); oCol.UniqueName = sUniqueName; oCol.DataMemberBinding = new System.Windows.Data.Binding(sValuefield); oCol.HeaderTextAlignment = TextAlignment.Center; oCol.TextAlignment = TextAlignment.Right; ColumnList.Add(oCol); i++; } if (this.OnCompletedEvent != null) { this.OnCompletedEvent(this, new EventArgs()); } } catch (Exception exc) { gUI.PopError(string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name), exc); }}
This works perfectly - ONCE
The second time i get the error
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in Telerik.Windows.Controls.GridView.dll
Additional information: DataGrid_DisplayIndexOutOfRange
Astonishingly there is little response from Google, or my foo is not working. Any ideas how to fix this problem.