I'm experiencing two issues with the wpf datagrid control. The first one: I have the main form, where some data is displayed, and another 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 return to normal.
Insummary:wheneverthemainformisloadedthishappensandafterafewseconds(oneortwo)thedataisdisplayedcorrectly.
Thexamldatagridcode:
<GridMargin="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've put only part of the code so it does not get too long; The data is loaded correctly and with satisfactory speed. The problem is loading as in the image above, it is always displayed first with the flattened columns and then normally.
What am I doing wrong? What is missing because I can not find anything wrong until then.
Thank you for your attention.