Search code examples
wpfwpfdatagridcolumnspan

Colspan in WPF DataGrid


I have a datagrid in one of my WPF application where I am showing some data from an XML file. Now there are rows which are kind of a headers. I want to show those rows with colspan, so that it occupies the whole row. I have tried the below code but it is not working.

<DataGrid.RowStyle>
  <Style TargetType="DataGridRow">    
      <Style.Triggers>
         <DataTrigger Binding="{Binding Type}" Value="BorderCheck">
              <Setter Property="Grid.ColumnSpan" Value="6" />
         </DataTrigger>                        
      </Style.Triggers>
  </Style>
</DataGrid.RowStyle>

Solution

  • There are 2 things if you need column span to all the rows there's no point adding extra columns if you need column span to particular rows you need set it at the row level which should be the element you want to expand to other columns like below

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="5*" />
                <ColumnDefinition Width="5*" />
            </Grid.ColumnDefinitions>
            <Label  Grid.Row="0" Grid.Column="0" FontSize="20" >Name</Label>
            <TextBox Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="5" />
        </Grid>
    

    Ouputwill be like this