Data in duplicate datagrid

1

I'm developing an application in WPF and when I populate the datagrid via code it returns the duplicate value. I'm using Entity Framework.

XAMLCode

<DataGridx:Name="dataLocal" Margin="10,111,11,10" IsReadOnly="True" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" Header="ID" Visibility="Hidden"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding CidadeInicial}" Header="Cidade Inicial"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding CidadeDestino}" Header="Cidade Destino"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Carga}" Header="Carga"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Kmrodado}" Header="KM Rodado"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Status}" Header="Status"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

C # code

private void preencheDGV()
    {
        //this.DataContext = local.dadosLocais.Local;
        dataLocal.ItemsSource = local.dadosLocais.Select(x => new
        {
            x.ID,
            x.CidadeInicial,
            x.CidadeDestino,
            x.Carga,
            x.Kmrodado,
            x.Status
        }).ToList();
    }

If you have a question,     

asked by anonymous 03.02.2018 / 01:57

1 answer

1

Set the property AutoGenerateColumns to false :

<DataGrid x:Name="dataLocal" Margin="10,111,11,10" IsReadOnly="True" SelectionMode="Single" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" Header="ID" Visibility="Hidden"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding CidadeInicial}" Header="Cidade Inicial"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding CidadeDestino}" Header="Cidade Destino"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Carga}" Header="Carga"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Kmrodado}" Header="KM Rodado"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Status}" Header="Status"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

On the other question, the correct thing is to ask another question, but just set the ColumnWidth property to * :

<DataGridTextColumn Binding="{Binding Status}" Header="Status" ColumnWidth="*">
</DataGridTextColumn>
    
03.02.2018 / 12:34