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

49,243

Solution 1

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.

Solution 2

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.

Share:
49,243
GilShalit
Author by

GilShalit

Currently owner and principal at DHDev where we do Digital Humanities development. Previously - Founder and VP R&amp;D at InnovizeIT, Leaders in DB2 application analysis and optimization (Mainframe and Open Systems)

Updated on August 12, 2022

Comments

  • GilShalit
    GilShalit over 1 year

    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?