Problem in wpf datagrid control display

Asked

Viewed 219 times

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. inserir a descrição da imagem aqui

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.

1 answer

0

For those who are facing the same problem as the newbie here the simplest solution is: do not declare the Width attribute in the xaml file. Click the main form, go to properties, click the "radius" icon (Events Handlers) and double-click 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;
}

Thus, 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 don’t always get so clear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.