throwingexceptions
throwingexceptions

Reputation: 21

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. 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:

     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

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. Now my XAML looks like this:

   <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

I have tried to bind my Results from my 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. 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?

Thanks in advance!

Upvotes: 0

Views: 5080

Answers (2)

I.Boe
I.Boe

Reputation: 1

< 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

Upvotes: 0

jonaamigo
jonaamigo

Reputation: 31

Where do you populate the 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

<DataGrid Grid.Row="1" Grid.ColumnSpan="3" ID="gridview" ItemsSource="{Binding}" >

Upvotes: 3

Related Questions