简体   繁体   English

DataGrid与LINQ结果绑定? WPF

[英]Datagrid binding with LINQ results? WPF

I am trying to bind(no code behind =)) my datagridview from my example WPF Application with my results of my LINQ. 我试图将我的示例WPF应用程序中的datagridview与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: 您必须考虑一些事情:首先,我正在使用ORACLE数据库,并且已经成功完成了映射,我得到了以下结果:

     this.SearchCommand = new RelayCommand(this.DisplayMessage, CanDisplayMessage);
    }


    public bool CanDisplayMessage()
    {
        return true;
    }

    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();
        }
    }

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

I know that I have to bind my Datagrid with an Itemssource, as I have always have made it. 我知道我必须像以前一样将Datagrid绑定到Itemssource。 I used an ObservableCollection to bind my results from my DataReader (with normal Sql Commands...) with my DataGrid. 我使用了一个ObservableCollection将我的DataReader的结果(使用普通的Sql Commands ...)与我的DataGrid绑定在一起。 Now my XAML looks like this: 现在,我的XAML如下所示:

   <DataGrid Grid.Row="1" Grid.ColumnSpan="3" ItemsSource="{Binding}" >
        <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>

http://s29.postimg.org/or9jhxuau/xaml.jpg http://s29.postimg.org/or9jhxuau/xaml.jpg

I have tried to bind my Results from my LINQ: 我试图从LINQ绑定结果:

  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();

        }
    }

with my datagrid but nothing is coming back! 与我的数据网格,但没有回来! I have tried to bind an Observerable Collection with this result but I am having an Error. 我试图用此结果绑定一个Observerable集合,但出现错误。 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? 我必须在DataGrid的ItemsSource上写些什么,以便可以将Datagrid与结果绑定在一起?

Thanks in advance! 提前致谢!

Where do you populate the gridview? 您在哪里填充gridview? Maybe this work ... 也许这项工作...

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();
     gridview.DataSource = results;
     gridview.Databind();
    }
}

You have to set an id here 你必须在这里设置一个ID

<DataGrid Grid.Row="1" Grid.ColumnSpan="3" ID="gridview" ItemsSource="{Binding}" >
< DataGrid ItemsSource="{Binding Path=results}">
   <DataGrid.Columns>
   <DataGridTextColumn Header="Employee ID"    Binding="{Binding EMPLOYEE_ID}"/>
   </DataGrid.Columns>

Items to be bind should be presented like public properties of ItemsSource 要绑定的项目应像ItemsSource的公共属性一样显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM