Volodymyr  Babak
Volodymyr Babak

Reputation: 27

RadGridView Dynamic columns adding

I'm trying to combine static and dynamic columns in my RadGridView. The problem is that dynamic column represents object with different properties that should be displayed within one cell.

This is how I add columns:

InitializeComponent();

        // Get some mock data
        ICollection <EmployeeRecord> employeeRecords = GetDummyData();

        this.grid.ItemsSource = employeeRecords;

        //Add the known columns
        this.grid.Columns.Add(new GridViewDataColumn() 
        { 
            UniqueName = "EmployeeName"
            , DataMemberBinding = new Binding("EmployeeName")
        });

        this.grid.Columns.Add(new GridViewDataColumn()
        {
            UniqueName = "ID"
            , DataMemberBinding = new Binding("ID")
        });


        // Now add the dynamic number of columns

        // Determines the maximum number of months that any employee has worked.
        int maxNumberOfMonths = employeeRecords.Max((x) => x.Subjects.Count);
        for (int i = 0; i < maxNumberOfMonths; i++)
        {
            this.grid.Columns.Add(new GridViewDataColumn()
            {
                UniqueName = "Subject " + (i + 1) ,
                DataMemberBinding = new Binding("Subjects[" + i + "]"),
                CellTemplate = this.Resources["SubjectTemplate"] as DataTemplate,
                DataType = typeof(Subject)
            });
        }

Data template for cell

    <DataTemplate x:Key="SubjectTemplate" DataType="ticket212220CreateGridColumnsOnTheFly:Subject">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Text="Subject"/>
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>

            <TextBlock Grid.Row="1" Grid.Column="0" Text="Mark"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Mark}"/>

        </Grid>
    </DataTemplate>

Model

 public class EmployeeRecord
{
    public string EmployeeName
    {
        get;
        set;
    }

    public int ID
    {
        get;
        set;
    }

    List<Subject> subjects = new List<Subject>();
    public IList<Subject> Subjects
    {
        get { return this.subjects; }
    }
}

public class Subject
{
    public string Name { get; set; }
    public int Mark { get;set; }
}

Generating fake data

      static ICollection<EmployeeRecord> GetDummyData()
    {
        ICollection<EmployeeRecord> employeeRecords = new List<EmployeeRecord>();

        EmployeeRecord stevie = new EmployeeRecord() { EmployeeName = "Steven Gerrard", ID = 333 };
        stevie.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });

        employeeRecords.Add(stevie);

        EmployeeRecord ryan = new EmployeeRecord() { EmployeeName = "Ryan Giggs", ID = 222 };
        ryan.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });
        ryan.Subjects.Add(new Subject
        {
            Name = "Geograph",
            Mark = 3
        });
        employeeRecords.Add(ryan);

        EmployeeRecord john = new EmployeeRecord() { EmployeeName = "John Terry", ID = 111 };
        john.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });
        john.Subjects.Add(new Subject
        {
            Name = "Geograph",
            Mark = 3
        });
        john.Subjects.Add(new Subject
        {
            Name = "Physics",
            Mark = 7
        });
        employeeRecords.Add(john);

        return employeeRecords;
    }

Any idea why Name and Mark are not displayed in cell?

Upvotes: 0

Views: 2155

Answers (1)

mm8
mm8

Reputation: 169420

The DataContext of a cell is an EmployeeRecord object since you set the ItemsSource property to an ICollection<EmployeeRecord>.

If you want to display all subjects for an employee in a column, you could use an ItemsControl that binds to the Subjects collection of the EmployeeRecord:

<DataTemplate x:Key="SubjectTemplate" DataType="ticket212220CreateGridColumnsOnTheFly:Subject">
    <ItemsControl ItemsSource="{Binding Subjects}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0 3 0 0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Subject" FontWeight="Bold" Margin="0 0 5 0"/>
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Mark" FontWeight="Bold" Margin="0 0 5 0"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Mark}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

Upvotes: 1

Related Questions