I have a datagrid displaying data from a table in a MySQL database. If all columns are with Width="Auto", when resizing the window the grid does not match. If I put it in the last column Width="*", it works as I wanted, following the resizing of the window, but opens with the last column overdue!
How can I get around this?
The code is as follows:
<Window x:Class="DataGridBind.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridBind"
mc:Ignorable="d"
Title="DataGridBind" SizeToContent="WidthAndHeight" >
<Grid Background="GhostWhite" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<GroupBox Header="Tabela: Produto" Grid.Row="0">
<StackPanel Orientation="Vertical">
<DataGrid AutoGenerateColumns="False" Height="Auto" Width="Auto" HorizontalAlignment="Left" Name="dataGridProdutos" ItemsSource="{Binding Path=carregarDados}" CanUserResizeRows="True" AlternatingRowBackground="GhostWhite" AlternationCount="2" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=idProduto}" Header="Código" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoNome}" Header="Nome" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoPU}" Header="Preço Unit." Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStock}" Header="Stock" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStockMin}" Header="Stock Min." Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=ProdutoStockMax}" Header="Stock Max." Width="Auto" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</GroupBox>
<GroupBox Header="Ações" Grid.Row="1" >
<StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Right">
<Button Content="Atualizar" Height="25" HorizontalAlignment="Left" Name="btnAtualizar" Width="100" Margin="5" Click="btnAtualizar_Click"/>
<Button Content="Sair" Height="25" HorizontalAlignment="Left" Name="btnSair" Width="100" Margin="5" Click="btnSair_Click" />
</StackPanel>
</GroupBox>
</Grid>