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!
DisplayMessage()
does something but useless here, the result is saved into the local variableresults
without any further usage. So how could it be used outside? – King King