Problem in displaying the wpf datagrid control

1

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.

    
asked by anonymous 08.12.2016 / 22:37

1 answer

0

For those who are facing the same problem as the novice here the simplest solution is: do not declare the Width attribute in the xaml file. Click the main form, go to properties, click on the "Events Handlers" icon and double-click on the SizeChanged method. Created the method for the application form, count how many columns your datagrid control will have and divide by the width of your main form. Example:

private void MyForm_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Size s = new Size();

    s = e.NewSize;

    int iWidth = (int)s.Width / (número de colunas do seu form);

    MyDatagrid.Column[0].Width = iWidth;
}

So regardless of the width of the form, the columns of the datagrid will be correct. Yeah, I know, it's simple ... but when you're learning things are not always so clear.

    
10.12.2016 / 01:51