in wpf how do i make a datagrid fit the window height

wpf datagrid
wpf grid size to content
wpf datagrid autosize columns
wpf datagrid column width
wpf datagrid row height
wpf datagrid column width fill
wpf datagrid resize columns
wpf datagrid resize with window

I have a grid with 3 columns and 2 rows

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

I the lower left cell, i have a data grid, with AutoGenerateColumns=True which can load many rows. What I want to do is for the data grid height to maximize to fit the window, and for the user to be able to use the datagrid scrollbar to scroll the rows up and down.

What happens is that the datagrid flows of the bottom of the window, and even if i set the

ScrollViewer.VerticalScrollBarVisibility="Visible"

of the datagrid, the scrollbar has no effect and the rows flow downwards. Somehow the datagrid does not feel restricted...

What to do?

Try setting your DataGrid's HorizontalAlignment=Stretch and VerticalScrollBarVisibility=Auto

If that doesn't work, you may also need to bind the Grid's Height to the Window Height so that it doesn't auto-grow to fit its contents. Usually I use Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}" (It might be RenderSize.ActualHeight instead of just ActualHeight... I forgot.

Another alternative is to use a DockPanel instead of a Grid since that control doesn't auto-grow to fit its contents. Instead it'll stretch its last child to fill the remaining space.

Sizing Options in the DataGrid Control, You can set you HorizontalAlignment=Stretch and VerticalScrollBarVisibility= Auto If this doesn't work,Bind the Grid's Height to the Window� When the Height is restricted, the DataGrid will only create the rows that will fit within its specified Height, and will recycle those rows as needed to display new data. Setting the DataGrid Size. The DataGrid can be set to automatically size within specified boundaries, or the DataGrid can be set to a specific size.

I had the same problem but binding to the Window height did not completely solve the problem for me. In my case the DataGrid still extended 2 to 3 inches below the Window's viewable area. I believe this was because my DataGrid started about 2 to 3 inches below the top of the Window.

In the end I found that it was not necessary to bind the DataGrid's height at all. All I had to do was change the DataGrid's direct container.

For me, the following XAML setup causes the DataGrid to extend beyond the size of the Window when enough rows are added. Notice that the DataGrid sits within a StackPanel.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- DataGrid within a StackPanel extends past the vertical space of the Window
     and does not display vertical scroll bars.  Even if I bind the height to Window 
     height the DataGrid content still extends 2-3 inches past the viewable Window area-->
    <StackPanel Grid.Row="1">
    <DataGrid ItemsSource="{StaticResource ImportedTransactionList}" 
         Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
    </StackPanel>
</Grid>

However, simply removing the StackPanel fixed the problem for me.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- Removing the StackPanel fixes the issue-->
    <DataGrid Grid.Row="1" ItemsSource="{StaticResource SomeStaticResource}" 
           Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
</Grid>

As the original post is quite old, I should note that I am using VS2017 and .Net Framework 4.6.1 but I am not sure if this has any bearing.

Resize of Datagrid based on Window Size - MSDN, Learn about Row Height Customization support in Syncfusion WPF DataGrid You can fit the row height based on its content in QueryRowHeight event handler You can reset height of the particular or all rows in View at runtime to get the <Window.Resources> <DataTemplate x:Key="headerTemplate"> <TextBlock� How to: Automatically Size a Window to Fit Its Content. 03/30/2017; 2 minutes to read +4; In this article. This example shows how to set the SizeToContent property to specify how a window resizes to fit its content.

You have to define the column and row where the DataGrid is with *. You say it is on the lower left cell. The row is ok but your column there has Width="Auto".

The AutoGenerateColumns=True gives a mess if Width="Auto".

Datagrid set height dynamically, I Have a DataGrid in my Window, and i need to automatically resize the column's width to fit content. But i have an obstacle: If his content is smaller than his� SizeToContent property of Window indicates whether a window will automatically size itself to fit the size of its content. This value is a type of SizeToContent enumeration that has four values – Manual, Width, Height, and WidthAndHeight. When SizeToContent is set to WidthAndHeight, the values of Height or Width properties have no effect.

Row Height Customization in WPF DataGrid control, To resolve this, the attached behaviour does a manual auto-fit of other (non last) Wpf datagrid column width autosize, Let's say you have window, 100 pixels� Set this property to NaN ("Auto" in XAML) to make the row automatically adjust its size to fit its content. Double.PositiveInfinity is not a valid value for this property. The value restrictions on the Double value are enforced by a ValidateValueCallback mechanism. If you attempt to set a value that is not valid, a run-time exception is thrown.

How to Resize Dynamically width of a WPF DataGrid Column?, You should be able to do it for a datagrid (or for any WPF control) by setting So, how can I get the window to keep it's size to content behavior after it's Try this, this should fit your needs: SizeToContent="WidthAndHeight" Height="Auto"� In a WPF project (VS2015) I have a DataGrid inside UserControl. When the DataGrid has enough items to scroll, the height of DataGrid goes beyond the UserControl's height and goes off-screen. I tried putting DataGrid in StackPanel, ScrollViewer, Grid, VerticalAlignment to Top/Stretch but no help.

How to make a wpf datagrid fill all available space and resize with , In this tutorial, you'll learn how to use a WPF DataGrid control, set its The Width and Height properties represent the width and the height of a DataGrid. The Margin property sets the margin of placement of DataGrid on the window. we will create a collection of objects and bind it to a DataGrid control. Having a DataGrid inside a grid, I would like to make the datagrid width and datagrid columns auto fit inside the parent control. So that if user decide to increase window width, how do I make the

Comments
  • Giving some width to data grid may solve your problem... if you don't want to hard code width you can do some kind of element binding to achieve this...
  • I'm worried about height. . .
  • sorry.. i am talking about height only.. its a typo... Giving some Height to data grid may solve your problem... if you don't want to hard code height you can do some kind of element binding to achieve this..
  • interesting ideas. I'll give them a try and report back :-)
  • The binding technique worked like a charm, Thanks! And it's ActualPath as you wrote. BTW, how do I bind something to a fraction of a Path value? Path=ActualPath/2 does not seem to work.
  • @Gilshalit You'll need to use a Converter for that
  • Note that this will cause the horizontal scrollbar not to show up even if you set ScrollViewer.HorizontalScrollBarVisibility="Auto"