1
I am facing two problems with wpf datagrid control. The first: I have the main form, where some data is displayed, and the other where the data is inserted. When I click to enter the data and the first form was hidden the control columns appear this way and then go back to normal.
In short: whenever the main form is loaded this happens and after a few seconds ( one or two ) the data is displayed correctly.
The Datagrid Shaman Code:
<Grid Margin="0,10,2,1.545">
<Grid.RowDefinitions>
<RowDefinition Height="24*"/>
<RowDefinition Height="239*"/>
<RowDefinition Height="229*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="dtDados" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" Grid.Row="2" FontWeight="Bold" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin" Margin="0,10,0,0" Sorting="dtDados_Sorting" MouseLeave="dtDados_MouseLeave">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ganho, Mode=TwoWay}" Value="Sim">
<Setter Property="Background" Value="DarkGray"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ganho, Mode=TwoWay}" Value="Não">
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<!-- Column Número entrada-->
<DataGridTextColumn Binding="{Binding n_entrada}" Header="Número Entrada" Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
<!-- Column Dados do usuário-->
<DataGridTextColumn Binding="{Binding d_usuario}" Header="Dados usuário" Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Application code:
//Método para inserção dos dados no datagrid
private void Principal_Loaded(object sender, RoutedEventArgs e)
{
try
{
//postgresql data base
sql.Open();
sqlCommand.CommandText = "SELECT ganho, n_entrada, d_usuario FROM usuario";
NpgsqlDataAdapter da00 = new NpgsqlDataAdapter(sqCommand);
DataTable dt00 = new DataTable();
da00.Fill(dt00);
dtDados.ItemsSource = dt00.DefaultView;
}
}
I put only a part of the code not to be extended; The data is loaded correctly and with satisfactory speed. The problem is the loading that as in the image above, always appears first with the flattened columns and then normally.
What am I doing wrong? What’s missing because I can’t find anything wrong until then.
Grateful for the attention.
You have already tried to remove the Width="*" attribute. If you do not pass the attribute(other than Width="") it may work.
– user26552