FrancescoDS
FrancescoDS

Reputation: 995

DataGrid with header for row and column

I would like to create a WPF Datagrid that has got header for rows and column, accordingly to the following excel grid:

enter image description here

In particular, the blue cells are fixed, that means that I suppose they needs to be the headers.

Notice that I would like to create a List that contains each row, and T is as follows:

class T
{
    double ag;
    double fZero;
    double tcStar;
}

How to create it with a DataGrid?

Upvotes: 1

Views: 7001

Answers (2)

VVN
VVN

Reputation: 1610

Use DataGrid.RowHeaderTemplate The DataGrid control has a RowHeaderTemplate property that defines the DataTemplate for the DataGridRowHeader.For columnheaders use ColoumnHeaderStyle.Check this link.

<DataGrid ItemsSource="{Binding Countries}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Country" Binding="{Binding Name}"/>
    </DataGrid.Columns>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DataContext.Continent, 
                        RelativeSource={RelativeSource AncestorType=DataGridRow}}"></TextBlock>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

Actually this sample sets the corresponding continent for each countries displayed in the rows.I think this link may help you

Upvotes: 3

itzmebibin
itzmebibin

Reputation: 9439

You can add column headers by using DataGridTemplateColumn.

  <DataGrid x:Name="dg" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Ag" Width="SizeToCells" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        //add items
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
<DataGridTemplateColumn Header="Fo" Width="SizeToCells" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        //add items
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
<DataGridTemplateColumn Header="Tc" Width="SizeToCells" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        //add items
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

You can use RowHeaderTemplate to add row headers.

Upvotes: 1

Related Questions