WPF C# datagrid create multiple column header

Asked

Viewed 215 times

0

I’m dynamically creating columns for a datagrid, but I need to create headers as below, some hint 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 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.

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.