如何防止外键加载到我的DataGrid

本文关键字:我的 DataGrid 加载 何防止 | 更新日期: 2023-09-27 18:01:12

现在,我正在获取表下的所有信息,并将其绑定到DataGrids。

然而,这使得我的外键在DataGrid上可见,这是我不想要的。此外,我也看不到Id列。

如何从数据库中选择某些列并将其绑定到DataGrid,而不是将所有数据绑定到DataGrid?

我想我只需要写一个查询,但它似乎不起作用。也许是因为我对实体框架还是个新手。

以下是我如何将数据存储到数据库中…:

 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((.Select((,但我想我不能在FirstOrDefault.之后执行Select Select。。。不知道为什么。

在我的课堂上设置外键的方式是:

    public virtual int LotInformationId { get;  set; }
    public virtual LotInformation LotInformation { get; set; }

但我不希望数据绑定到我的dataGrid。。。

希望这是清楚的。如果你有任何问题,请问我。

谢谢!

如何防止外键加载到我的DataGrid

正如Michael Perrenoud所说,您可以在xaml:中指定所需的列

  <DataGrid ItemSource="{Binding YourCollection}" SelectedItem="{Binding YourCurrentSelectedItem}">
      <DataGrid.Columns>
         <DataGridTextColumn Header="SomeColumnHeader" Binding={Binding SomePropertyOnTheModel} />
      </DataGrid.Columns>
  </DataGrid>

如果您不需要某些列,则需要手动设置这些列并设置它们的数据绑定,而不是允许auto-columns属性工作。