现在,我正在获取表格下的所有信息并将其绑定到我的DataGrids。
然而,这使得我的外键在DataGrid上可见,这是我不想要的。另外,我也不会看到Id列。
如何从数据库中选择某些列并将其绑定到我的DataGrid而不是将所有数据绑定到我的DataGrid上?
我想我只需编写一个查询但看起来它不起作用。也许是因为我还是Entity Framework的新手。
以下是我将数据存储到db ...中的方式:
using (var db = new DMIDataContext())
{LotInformation newLot = new LotInformation();
newLot.Id = lot.Id;
newLot.lot_number = lot.lot_number;
newLot.exp_date = lot.exp_date;
foreach (Components comp in lot.Components)
{
newLot.Components.Add(comp);
}
ComponentsList = newLot.Components;
foreach (Families fam in lot.Families)
{
newLot.Families.Add(fam);
}
Families = newLot.Families;
db.LotInformation.Add(newLot);
db.SaveChanges();
以下是我抓取数据库数据的方法:
public static void ReadLot(string lotNumber)
{
using (var db = new DMIDataContext())
{
try
{
LotInformation lotInfo = db.LotInformation.FirstOrDefault(r => r.lot_number.Equals(lotNumber));
}
catch (InvalidOperationException e)
{
}
}
}
我认为问题在于上面的查询...我尝试使用FirstOrDefault()。选择()但我想我不能做一个选择在FirstOrDefault之后选择...不知道为什么。
我班上设置外键的方式是:
public virtual int LotInformationId { get; set; }
public virtual LotInformation LotInformation { get; set; }
但我不希望数据绑定到我的dataGrid ...
希望这很清楚。如果您有任何疑问,请询问我。
谢谢!
答案 0 :(得分:2)
就像Michael Perrenoud所说,您可以在xaml中指定所需的列:
<DataGrid ItemSource="{Binding YourCollection}" SelectedItem="{Binding YourCurrentSelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Header="SomeColumnHeader" Binding={Binding SomePropertyOnTheModel} />
</DataGrid.Columns>
</DataGrid>
答案 1 :(得分:1)
如果您不想要某些列,则需要手动设置列并设置其数据绑定,而不是允许自动列属性工作。