This is a migrated thread and some comments may be shown as answers.

Dynamic columns in a treelistviw

1 Answer 159 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 26 May 2017, 06:26 AM

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. 

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 30 May 2017, 07:14 AM
Hello Mark,

I tried to reproduce the exception you described in a sample project, but was, unfortunately, unable to do so.

Could you please have a look at the project I've attached to my reply and let me know if you can manage to replicate the exception with it?

If you do not find this possible, please open a new support ticket and send over a sample project of your own so that I can further investigate and possibly provide you with a solution.

Thank you in advance for your cooperation.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or