如何将EF dataSet与WPF数据网格绑定?

时间:2021-07-09 08:53:52

I can't connect properly my dataSet (from EF) with WPF datagrid. My issue is that my datagrid shows :

我无法正确连接我的dataSet(来自EF)和WPF数据网格。我的问题是我的数据网格显示:

  • two selected column with correct column header (great :-) ! )
  • 两个选定的列具有正确的列标题(伟大的:-)! )

  • all other column (including two above) with column header from database (I don't want theses columns)
  • 所有其他列(包括上面两个)与数据库中的列标题(我不想要这些列)

Here is my code :

这是我的代码:

<DataGrid  Name="dataGridDomaines" Grid.Row="4" IsReadOnly="True" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Nom Domaine" Width="300" Binding="{Binding Path=NomDomaine}"></DataGridTextColumn>
            <DataGridTextColumn Header="N° Inter" Width="100" Binding="{Binding Path=idOpérateur}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

And :

private void Rechercher(string elementARechercher){
        using (CaveAVinContext ctx = new CaveAVinContext())
        {
            //recherche des domaines
            var domaines = ctx.Domaines
                .Where(a => a.NomDomaine.Contains(elementARechercher))
                .ToList();
            dataGridDomaines.ItemsSource = domaines;
            lblDomaines.Content = "Domaine(s) : " + domaines.Count + " resultat(s)";


        }
    }

What's wrong ?

怎么了 ?

2 个解决方案

#1


By default, the AutoGenerateColumns property of a DataGrid is true, which will cause it to append all of the columns to the end of whatever you've specified directly.

默认情况下,DataGrid的AutoGenerateColumns属性为true,这将导致它将所有列追加到您直接指定的任何列的末尾。

Just set that to false and you should be good to go.

把它设置为假,你应该好好去。

<DataGrid  Name="dataGridDomaines" Grid.Row="4" 
       IsReadOnly="True" ItemsSource="{Binding}"
       AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Nom Domaine" Width="300" Binding="{Binding Path=NomDomaine}"></DataGridTextColumn>
        <DataGridTextColumn Header="N° Inter" Width="100" Binding="{Binding Path=idOpérateur}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

#2


Set Datagrid Autogeneratecolumns to False. * Note that The default value for this property is True.

将Datagrid Autogeneratecolumns设置为False。 *请注意,此属性的默认值为True。

#1


By default, the AutoGenerateColumns property of a DataGrid is true, which will cause it to append all of the columns to the end of whatever you've specified directly.

默认情况下,DataGrid的AutoGenerateColumns属性为true,这将导致它将所有列追加到您直接指定的任何列的末尾。

Just set that to false and you should be good to go.

把它设置为假,你应该好好去。

<DataGrid  Name="dataGridDomaines" Grid.Row="4" 
       IsReadOnly="True" ItemsSource="{Binding}"
       AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Nom Domaine" Width="300" Binding="{Binding Path=NomDomaine}"></DataGridTextColumn>
        <DataGridTextColumn Header="N° Inter" Width="100" Binding="{Binding Path=idOpérateur}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

#2


Set Datagrid Autogeneratecolumns to False. * Note that The default value for this property is True.

将Datagrid Autogeneratecolumns设置为False。 *请注意,此属性的默认值为True。