LINQ填充DataGrid,ItemsSource绑定问题 [英] LINQ populate DataGrid, ItemsSource binding problem

查看:79
本文介绍了LINQ填充DataGrid,ItemsSource绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试将我的示例WPF应用程序中的datagridview与LINQ的结果绑定(后面没有代码).它一定很容易,但是我莫名其妙"地错过了它.某处.您必须考虑的一些事情:首先,我正在使用ORACLE数据库 并且我已经成功完成了映射,我得到了以下结果:

Hello guys,

I am trying to bind(no code behind =)) my datagridview from my example WPF Application with my results of my LINQ. It must be pretty easy but I am missing it "somehow" somewhere. Some things you must consider: First of all I am using an ORACLE db and I have successfully made the mappings, I am having this result:

http://s27.postimg.org/ya0crw701/linqresults.jpg

http://s27.postimg.org/ya0crw701/linqresults.jpg

在代码段上,您可以看到我的linq命令(考虑一下,当我单击搜索"按钮时,要知道DisplayMessage只是从RelayCommand调用的方法).如您所见,我在代码之前的链接上得到了结果 片段.

on the code snippet you can see my linq command (consider to know that DisplayMessage is only a method that is called from a RelayCommand, when i have Click on "Search" button). As you can see I am having my results on the link before the code snippet.

  public void DisplayMessage()
    {
       using ( Entities ctx = new Entities())
        {
            var query = from e in ctx.EMPLOYEES select new { e.EMPLOYEE_ID,e.FIRST_NAME,
                         e.LAST_NAME, e.EMAIL, e.PHONE_NUMBER,
                         e.SALARY, e.DEPARTMENT_ID};

         var results = query.ToList();
        }

现在这也是我的XAML:

now this is also my XAML:

<DataGrid Grid.Row="1" Grid.ColumnSpan="3" ItemsSource="{Binding results}" >
        <DataGrid.Columns>
        <DataGridTextColumn Header="Employee ID"    Binding="{Binding Path= EMPLOYEE_ID}"/> 
        <DataGridTextColumn Header="First Name"     Binding="{Binding Path= FIRST_NAME}"/>
        <DataGridTextColumn Header="Last Name"      Binding="{Binding Path= LAST_NUMBER}"  />
        <DataGridTextColumn Header="Email"          Binding="{Binding Path= EMAIL}"  />
        <DataGridTextColumn Header="Phone number"   Binding="{Binding Path= PHONE_NUMBER}"/>
        <DataGridTextColumn Header="Salary"         Binding="{Binding Path= SALARY}"  />
        <DataGridTextColumn Header="Department ID"  Binding="{Binding Path= DEPARTMENT_ID}"  />
        </DataGrid.Columns>
    </DataGrid>

我知道我必须将Datagrid绑定到Itemssource,就像我一直以来所做的那样.我使用了一个ObservableCollection将我的DataReader的结果(使用常规的Sql Commands ...)与我的DataGrid绑定在一起.我试图绑定一个可观察的集合 出现此结果,但我遇到错误(错误1无法将类型'System.Collections.Generic.List< AnonymousType#1>'隐式转换为'System.Collections.Generic.IEnumerable< EMPLOYEE>'.存在显式转换 (您是否错过了演员表?)我之前尝试过,但是出现了.我在这里想念的是什么?我必须在DataGrid的ItemsSource上写些什么,以便可以将Datagrid与结果绑定在一起?

我的洞项目在这里:
http://www.mediafire.com/download/csdq3djqd25w8xo/EFWPFEZ.7z

I know that I have to bind my Datagrid with an Itemssource, as I have always have made it. I used an ObservableCollection to bind my results from my DataReader (with normal Sql Commands...) with my DataGrid. I have tried to bind an Observerable Collection with this result but I am having an Error (Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<EMPLOYEE>'. An explicit conversion exists (are you missing a cast?) I have tried that before but this comes up). What I am missing here? What I have to write on ItemsSource of my DataGrid so I can bind the the Datagrid with my results?

my hole project is here:
http://www.mediafire.com/download/csdq3djqd25w8xo/EFWPFEZ.7z

我正在使用Oracle DB和MVVM Light.

I am using Oracle DB and MVVM Light.

提前谢谢!

推荐答案

您需要将linq查询结果转换为可观察的集合或任何IEnumerable< T>.请参阅下面的链接.

You need to cast linq query result to observable collection or any IEnumerable<T>. Refer below link.

http://stackoverflow.com/questions/21911207/casting-a-linq-query-to-observablecollection

http://stackoverflow.com/questions/21911207/casting-a-linq-query-to-observablecollection

http://stackoverflow.com/questions/1465285/cast-linq-result-to-observablecollection

http://stackoverflow.com/questions/1465285/cast-linq-result-to-observablecollection


这篇关于LINQ填充DataGrid,ItemsSource绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆