27

How to make a wpf datagrid fill all available space and resize with window? Mind that the datagrid is inside a grid.

2
  • Show your code. Your parent grid has to be set up properly as well depending on the code. Dec 17, 2013 at 3:48
  • As @dirt requested please show the code. Generally I don't use Grid for this particular reason. Whenever I need my controls to be dynamically re-sized I use DockPanel and StackPanel to arrange my controls. It provides a lot of flexibility since DockPanel fills the available space with the last defined logical child
    – Sandesh
    Dec 17, 2013 at 6:38

5 Answers 5

30

You should be able to do it for a datagrid (or for any WPF control) by setting HorizontalAlignment and VerticalAlignment to Stretch

If it's inside a Grid, you should be able to set something like this

<Grid> <!-- parent grid or whatever -->
   <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ... >
   </DataGrid>
</Grid>
2
  • 3
    You might also want to set SizeToContent="WidthAndHeight" on your Window control. Oct 4, 2016 at 14:36
  • 1
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" declarations are useless since Stretch is the default value.
    – Orace
    Mar 16, 2022 at 9:56
11
  1. Never set height or width to it or its parent control
  2. No need to worry on the horizontal and vertical alignment since by default Horizontal and vertical alignment are stretch. if above are proper things should work :)
0
4

Set the width for the last column of the DataGrid to *.

<DataGridTextColumn Header="Notes"
                    Binding="{Binding Notes}"
                    Width="*"/>

Note : This actually works on only the last column of the DataGrid

3
  • The * star is an invalid input for the Width Mar 25, 2019 at 17:43
  • 1
    @MohamedKamelBouzekria, they why does it do exactly what I want it to do? Mar 26, 2019 at 13:21
  • @MohamedKamelBouzekria, thanks for the edit, it makes sense now. Mar 26, 2019 at 16:45
2

Remove all height and width property of its parent control. remove horizontal and vertical property. Define row height as * of its parent grid.

<Grid> 
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
   <DataGrid Grid.Row="0" >
   </DataGrid>
</Grid>
2
  • 3
    It is not a Grid, it is DataGrid
    – Xav987
    Sep 30, 2016 at 16:55
  • The DataGrid is located in the Grid.
    – Jeff
    Oct 25, 2022 at 18:39
-1

Insert DataGrid directly to the Grid without wrapping with a panel.

    <Grid>
       <DataGrid>
       </DataGrid>
    </Grid>
2
  • This won't work as the DataGrid will take as much space as it needs and not fill all the space available Mar 25, 2019 at 17:44
  • 1
    Ironically, this is the correct answer.
    – Orace
    Mar 16, 2022 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.