我试图绑定(没有代码隐藏=))我的示例WPF应用程序中的datagridview和我的LINQ结果。它一定很容易,但我在某处“某种程度上”错过它。你必须考虑的一些事情:首先我使用的是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
我知道我必须使用Itemssource绑定我的Datagrid,因为我一直都是这样做的。我使用ObservableCollection将我的DataReader(带有正常的Sql命令...)的结果与我的DataGrid绑定。现在我的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
我试图从我的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();
}
}
我的数据网格但没有回来!我试图用这个结果绑定一个Observerable Collection,但是我遇到了一个错误。我在这里缺少什么?我必须在DataGrid的ItemsSource上编写什么才能将Datagrid与我的结果绑定?
提前致谢!
答案 0 :(得分:1)
您在哪里填充gridview? 也许这项工作......
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();
}
}
你必须在这里设置一个id
<DataGrid Grid.Row="1" Grid.ColumnSpan="3" ID="gridview" ItemsSource="{Binding}" >
答案 1 :(得分:0)
< DataGrid ItemsSource="{Binding Path=results}">
<DataGrid.Columns>
<DataGridTextColumn Header="Employee ID" Binding="{Binding EMPLOYEE_ID}"/>
</DataGrid.Columns>
要绑定的项目应该像ItemsSource的公共属性一样显示