0
votes

I want to display the "Columns" data of object DataRow in a GridView:

public class DataRow 
{
    public string Row { get; set; }
    public ObservableCollection<IDictionary<string, object>> Columns { get; set; }

    public DataRow(string row, ExpandoObject columnData)
    {
        this.Row = row;
        this.Columns = new ObservableCollection<IDictionary<string, object>>() { columnData as IDictionary<string,object>};
    }
}

All my DataRow objects are collected in an ObservableCollection in another class:

public class Table
{
    public string DataTableName{ get; set; }
    public ObservableCollection<DataRow> Data{ get; set; }
}

And all my Tables are contained in a ObservableCollection in another class:

public class MyTables
{
   public ObservableCollection<Table> MyTables{ get; set; }
}

Thus I've tried to do my binding the following way:

<ItemsControl ItemsSource="{Binding MyTables}">
   <ItemsControl.ItemTemplate>
        <DataTemplate>
             <Grid>
                  <Grid.RowDefinitions>
                        ...
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                        ...
                  </Grid.ColumnDefinitions>

                  <Label ...Text="{Binding DataTableName}"/>

                  <telerik:RadGridView
                        ...
                        AutoGenerateColumns="True"
                        ItemsSource="{Binding Data.Columns}"/>
              </Grid>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
  </ItemsControl>

I don't understand why I can't bind my ItemsSource of the GridView to the Data.Columns- ObservableCollection.

Error message:

BindingExpression path error: 'Columns' property not found on 'object' ''ObservableCollection`1' (HashCode=61300126)'. BindingExpression:Path=Data.Columns; DataItem='Table' (HashCode=50904493); target element is 'RadGridView' (Name=''); target property is 'ItemsSource' (type 'Object')

Beside only displaying the data, the table should support copy/paste from Excel and editing of values. Because I don't know in advance how much rows and columns are needed I'm using the dynamic ExpandoObject where I can add Properties at runtime which are my columns in the data grid.

2

2 Answers

0
votes

The Object called "Data" is an ObservableCollection of DataRow. As mentionned in the error you got, this object has no property called "Columns".
If you want to display Columns for each DataRow in Data, you might want to use a hierarchical grid where you display "Row" property and the Columns data as a child grid. See https://docs.telerik.com/devtools/wpf/controls/radgridview/getting-started/building-hierarchical-grid-view

0
votes

According to Kilians last comment I flattened the data source and integrated "Columns" in my Table class. This is working and results in following data table:

Displayed dynamic table

This table fulfills my requirement to display the data and supports copy/paste from Excel. I also can edit the cells. If I'm selecting now a cell of this data grid I know which column is selected but unfortunately I'm missing the row- information. I can bypass the problem if every cell has a unique value, then I can do comparisons to find the currently edited row. But what could I do if the that isn't the case?