WPF C # datagrid create header multiple columns

0

I am creating dynamically columns for a datagrid, however I need to create headers like below, any tips on how to do using C #?

---------------------------------------------
|       Person 1      |       Person 2      |
| Column 1 | Column 2 | Column 1 | Column 2 |
---------------------------------------------
|Data 1    |Data 2    |Data 3    |Data 4    |
---------------------------------------------
|Data 5    |Data 6    |Data 7    |Data 8    |
---------------------------------------------

I'm using the example below to add columns and rows, but I have no idea how to add the multiple header.

public class Item
{
    public int Num { get; set; }
    public string Start { get; set; }
    public string Finich { get; set; }
}

private void generate_columns()
{
    DataGridTextColumn c1 = new DataGridTextColumn();

    c1.Header = "Num";
    c1.Binding = new Binding("Num");
    c1.Width = 110;

    dataGrid1.Columns.Add(c1);

    DataGridTextColumn c2 = new DataGridTextColumn();

    c2.Header = "Start";
    c2.Width = 110;
    c2.Binding = new Binding("Start");

    dataGrid1.Columns.Add(c2);

    DataGridTextColumn c3 = new DataGridTextColumn();

    c3.Header = "Finish";
    c3.Width = 110;
    c3.Binding = new Binding("Finish");

    dataGrid1.Columns.Add(c3);

    dataGrid1.Items.Add(new Item() { Num = 1, Start = "2012, 8, 15", Finish= "2012, 9, 15" });
    dataGrid1.Items.Add(new Item() { Num = 2, Start = "2012, 12, 15", Finish= "2013, 2, 1" });
    dataGrid1.Items.Add(new Item() { Num = 3, Start = "2012, 8, 1", Finish = "2012, 11, 15" });

}

Thank you.

    
asked by anonymous 10.08.2017 / 20:26

1 answer

0
<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Officer11Nome}"
                               HorizontalAlignment="Center" 
                               Grid.Column="0"
                               Grid.Row="0"
                               Grid.ColumnSpan="3"
                               />
            <TextBlock Text="Abertura" 
                               HorizontalAlignment="Center" 
                               Grid.Column="0"
                               Grid.Row="1"
                               />

            <TextBlock Text="Fechamento" 
                               HorizontalAlignment="Center" 
                               Grid.Column="2"
                               Grid.Row="1"
                               />
        </Grid>
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Officer1Percentagem}" TextAlignment="Center" Grid.Column="0" Grid.ColumnSpan="3"/>
                <TextBlock Text="{Binding Officer1Abertura}" TextAlignment="Right" Grid.Column="0"/>
                <TextBlock Text="{Binding Officer1Fechamento}" TextAlignment="Right" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Officer12Nome}"
                               HorizontalAlignment="Center" 
                               Grid.Column="0"
                               Grid.Row="0"
                               Grid.ColumnSpan="3"
                               />
            <TextBlock Text="Abertura" 
                               HorizontalAlignment="Center" 
                               Grid.Column="0"
                               Grid.Row="1"
                               />

            <TextBlock Text="Fechamento" 
                               HorizontalAlignment="Center" 
                               Grid.Column="2"
                               Grid.Row="1"
                               />
        </Grid>
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Officer2Percentagem}" TextAlignment="Center" Grid.Column="0" Grid.ColumnSpan="3"/>
                <TextBlock Text="{Binding Officer2Abertura}" TextAlignment="Right" Grid.Column="0"/>
                <TextBlock Text="{Binding Officer2Fechamento}" TextAlignment="Right" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
    
23.08.2017 / 17:41