title: How to have DataGrid to Fill Up the Blank Space in Window in WPF
body: <p>I am working on a project in WPF. In Which my requirement is to attach my DataGrid at the bottom of the Window, so that if the users will Restore Down or Maximize the window or change the size of the window, the DataGrid will should not go beyond the main window in terms of height and should resize automatically and scroll bars should appear view the data. In the code given below the DataGrid is only displays the data available for example if there are only 2 rows to display it will show only 2 row in the datagrid and remaining users can see the background and if the data is having 50 60 rows the data grid will go beyond the main window and users cannot see all the rows in the Data Grid. </p>
  
  <p>My Requirement
  Datagrid Height should not go beyond than the available space in the Main Window. 
  If there are no rows to display in the DataBase then DataGrid Should show Empty Rows or Simply Gray Space. </p>
  
  <pre><code>&lt;StackPanel&gt;
  
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;TextBlock Margin="40 50 0 0" Text="Label1:" Foreground="White" FontWeight="Medium" /&gt;
          &lt;ComboBox x:Name="CustomerList" Margin="15 40 0 0" Padding="7" Width="300" Foreground="Black" FontWeight="Medium" Cursor="Hand"&gt;
      &lt;/StackPanel&gt;
  
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;TextBlock Margin="25 30 0 0" Text="Label2" Foreground="White" FontWeight="Medium" TextWrapping="Wrap" Width="54"/&gt;
          &lt;TextBox x:Name="AppName" Margin="15 30 0 0" Padding="7" Width="230" Background="LightCyan" Foreground="Black" FontWeight="Medium" /&gt;
      &lt;/StackPanel&gt;
  
      &lt;StackPanel Orientation="Horizontal"&gt;          
          &lt;TextBox x:Name="Tenure" Margin="20 30 0 0" Padding="7" Width="170" Background="LightCyan" Foreground="Black" FontWeight="Medium" /&gt;          
      &lt;/StackPanel&gt;
  
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="1000"/&gt;
              &lt;ColumnDefinition Width="150"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;StackPanel&gt;
              &lt;StackPanel Orientation="Horizontal" Grid.Column="0"&gt;
  
                 &lt;TextBlock Margin="25 30 0 0" Text="Label2.:" Foreground="White" FontWeight="Medium" Width="80" TextWrapping="Wrap"/&gt;
              &lt;/StackPanel&gt;
  
              &lt;StackPanel Orientation="Horizontal" Grid.Column="0"&gt;                    
                  &lt;Button x:Name="Add_btn" Content="Add Data" Width="120" Padding="12" Background="Transparent" Foreground="White" FontWeight="Medium" Margin="15 30 0 0" BorderBrush="White" BorderThickness="1" Click="Add_btn_Click" Cursor="Hand"/&gt;
              &lt;/StackPanel&gt;
          &lt;/StackPanel&gt;
  
          &lt;StackPanel Grid.Column="1" HorizontalAlignment="Right"&gt;
              &lt;Image Source="C:\Users\Asim Abbas\Source\Icons\Upload1.png" Height="90" Width="90" Margin="0 25 0 0" MouseLeftButtonDown="Image_MouseLeftButtonDown" Cursor="Hand"&gt;
                  &lt;Image.Effect&gt;
                      &lt;DropShadowEffect/&gt;
                  &lt;/Image.Effect&gt;
              &lt;/Image&gt;
  
              &lt;TextBlock Margin="20,5,0,0" Text="Upload" Foreground="White" FontWeight="Medium" /&gt;
  
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>This is The Point Where Thing Gets Ugly - The Data Is Continuing from Above</p>
  
  <pre><code>    &lt;DataGrid 
          DockPanel.Dock="Bottom"
          x:Name="Grid"
          Margin="0 20 0 0"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          CanUserDeleteRows="False"
          ColumnHeaderHeight="60"
          CanUserResizeColumns="False"
          ScrollViewer.VerticalScrollBarVisibility="Visible"
          VerticalScrollBarVisibility="Visible"
          CanUserReorderColumns="False"
          CanUserResizeRows="False"
          CanUserSortColumns="False"
          RowBackground="LightCyan"
          RowHeight="30"
          HorizontalGridLinesBrush="LightGray"
          Width="Auto"            
          IsReadOnly="True"
          VerticalAlignment="Stretch"
          VerticalContentAlignment="Stretch"
          FontSize="11"
          HorizontalAlignment="Left"
          FontWeight="Medium"
          VerticalGridLinesBrush="LightGray" 
          BorderBrush="FloralWhite"
          MouseDoubleClick="LCGrid_MouseDoubleClick"&gt;
  
          &lt;DataGrid.ColumnHeaderStyle&gt;
              &lt;Style TargetType="DataGridColumnHeader"&gt;
                  &lt;Setter Property="ContentTemplate"&gt;
                      &lt;Setter.Value&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{Binding}"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
                  &lt;Setter Property="VerticalContentAlignment" Value="Bottom"/&gt;
                  &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
                  &lt;Setter Property="BorderThickness" Value=".5,.5,.5,.5"/&gt;
                  &lt;Setter Property="BorderBrush" Value="LightGray"/&gt;
                  &lt;Setter Property="Background" Value="FloralWhite"/&gt;
                  &lt;Setter Property="FontWeight" Value="SemiBold"/&gt;
                  &lt;Setter Property="FontSize" Value="12"/&gt;
                  &lt;Setter Property="Padding" Value="5"/&gt;
                  &lt;Setter Property="Width" Value="Auto"/&gt;
              &lt;/Style&gt;
          &lt;/DataGrid.ColumnHeaderStyle&gt;
  
          &lt;DataGrid.RowHeaderStyle&gt;
              &lt;Style TargetType="DataGridRowHeader"&gt;
                  &lt;Setter Property="HorizontalContentAlignment" Value="Right"/&gt;
                  &lt;Setter Property="Width" Value="25"/&gt;
                  &lt;Setter Property="FontWeight" Value="Medium"/&gt;
                  &lt;Setter Property="FontSize" Value="12"/&gt;
                  &lt;Setter Property="Background" Value="FloralWhite"/&gt;
              &lt;/Style&gt;
          &lt;/DataGrid.RowHeaderStyle&gt;
  
          &lt;DataGrid.CellStyle&gt;
              &lt;Style TargetType="DataGridCell"&gt;
                  &lt;Setter Property="Padding" Value="3"/&gt;
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                              &lt;Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" VerticalAlignment="Bottom"&gt;
                                  &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Bottom"/&gt;
                              &lt;/Border&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/DataGrid.CellStyle&gt;
  
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 1" Width="150"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col2" Width="60"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat={}{0:dd-MMM-yy}}" Header="Col 3" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col3" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 4" Width="250"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col5" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N2}" Header="LC Value" Width="100"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Right"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N2}" Header="Col6" Width="100"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Right"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N2}" Header="Col 7" Width="100"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Right"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N2}" Header="Col8" Width="100"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Right"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N2}" Header="Col9" Width="100"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Right"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat={}{0:dd-MMM-yy}}" Header="Col10" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat={}{0:dd-MMM-yy}}" Header="Col11" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col12" Width="250"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N0}" Header="Col13" Width="80"&gt;
  
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1, StringFormat=N0}" Header="Col 14" Width="80"&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="TextBlock"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 15" Width="150"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 16" Width="150"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 17" Width="200"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 18" Width="80"/&gt;
              &lt;DataGridTextColumn Binding="{Binding Col1}" Header="Col 19" Width="100"/&gt;
  
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p></p>