将数据绑定到 Datagridview 的特定列 - Entity Framework

Bind Data to Specific Columns of Datagridview - Entity Framework

我不知道我是应该获取所有数据还是只获取我需要的数据:) 我是第一次这样做。 我想将此数据绑定到特定列

见select

var SQLquery = (from artikel in db.DHH_Lagerverwaltung_Artikel
join hersteller in db.DHH_Lagerverwaltung_Hersteller on artikel.ID_Hersteller equals hersteller.ID_Hersteller
join kategorie in db.DHH_Lagerverwaltung_Kategorie on artikel.ID_Kategorie equals kategorie.ID_Kategorie
join bestand in db.DHH_Lagerverwaltung_Bestand on artikel.ID_Artikelnummer equals bestand.ID_Artikelnummer
join fach in db.DHH_Lagerverwaltung_Fach on bestand.ID_Fach equals fach.ID_Fach
join stellplatz in db.DHH_Lagerverwaltung_Stellplatz on fach.ID_Stellplatz equals stellplatz.ID_Stellplatz
join ebene in db.DHH_Lagerverwaltung_Ebene on stellplatz.ID_Ebene equals ebene.ID_Ebene
join regal in db.DHH_Lagerverwaltung_Regal on ebene.ID_Regal equals regal.ID_Regal
join lager in db.DHH_Lagerverwaltung_Lager on regal.ID_Lager equals lager.ID_Lager
//where lager.Raum == ""
select new {
 ArtikelBezeichnung = artikel.Bezeichnung,
  ArtikelEAN = artikel.EAN,
  BestandsMenge = bestand.Menge,
  MinMenge = bestand.Menge,
  Lagerort = lager.Raum + regal.RegalNr + ebene.Ebene + stellplatz.Stellplatz + fach.Fach,
  Hersteller = hersteller.Name,
  Kategorie = kategorie.Name
});

在查询下方执行这一行代码:

dataGridViewX.DataSource = new BindingSource(SQLquery.ToList(), null);

BindingSource 可以与 List<anonymoustype> 查询将创建的


或者,因为您使用的是匿名类型,所以您也可以创建一个扩展方法来为您生成一个 BindingList:

static class ListExtensions
{
    public static BindingList<T> ToBindingList<T>(this IList<T> source)
    {
        return new BindingList<T>(source);
    }
}

您可以将 datagridview 绑定到 bindingList:

dataGridViewX.DataSource = SQLquery.ToList().ToBindingList();

通过 BindingSource 进行绑定为过滤、排序、访问当前项目等提供了一些优势。它还允许您安排分层数据结构。如果您要使用 BindingSource,您或许应该考虑不使用匿名类型,因为它们是编译器生成的 POCO classes,您实际上没有任何可靠的访问权限 if you wanted to dig your bindingSource's .Current object out and cast it to something you work with.

如果您在自己的代码中将 class 设为完全定义的代码,那么您将:

collection.Select(c => new Whatever(){ Id = c.Id, Name = c.Name });

但您可以更好地使用它:

var x = myBindingSource.Current as Whatever;

如果你使用匿名类型,那就是你不能轻易做到的 as Whatever 类型转换,你最终会陷入 myBindingsource.Current 作为对象的困境,需要一些 dynamic 解决方法(当这实际上是设计时已知的 class 类型时,这不是最佳选择)或一些 hack,您声明另一个具有相同顺序和参数类型的匿名类型,并依赖于编译器使它们成为创建匿名类型时也是一样

您好,我创建了一个小的演示代码。您只需要重新编写代码,它就可以工作。希望对你有帮助。

private class Data
    {
        public int? Id { get; set; }
        public DateTime? DateTimeShipped { get;set;}
        public DateTime? DontNeed1 { get; set; }
        public DateTime? DontNeed2 { get; set; }
        public bool? Ok { get; set; }

        public Data()
        {

        }
        public Data(int? id, DateTime? dateTime, bool? Ok, DateTime? DontNeed1, DateTime? DontNeed2)
        {
            this.Id = id;
            this.DateTimeShipped = dateTime;
            this.Ok = Ok;
            this.DontNeed1 = DontNeed1;
            this.DontNeed2 = DontNeed2;
        }
    }

Class 数据保存来自 linq select 的值。 还有字典,其中包含所需列的列表,包括它们的类型。此字典用于创建应在 DataGridView 中的列。

Dictionary<string, Type> desiredCols = new Dictionary<string, Type>
{
     {"Id", typeof(int)},
     {"DateTimeShipped", typeof(DateTime)},
     { "Ok", typeof(bool)}
};

IEnumerable<Data> sqlList = (from data in dbContext.SomeTable
                  select new Data
                  {
                      Id = data.Id,
                      DateTimeShipped = data.DatumSuggestFrom,
                      Ok = data.Ok,
                      DontNeed1 = data.DatumSuggestFrom,
                      DontNeed2 = data.DatumSuggestTo
                  }).ToList();

var bindingList = new BindingList<Data>(sqlList.ToList());
foreach(var c in desiredCols)
{
    DataGridViewColumn col = new DataGridViewTextBoxColumn();
    col.Name = c.Key;
    col.HeaderText = c.Key;
    col.ValueType = c.Value;
    dataGridView1.Columns.Add(col);
 };

 foreach(var col in bindingList)
 {
     dataGridView1.Rows.Add(col.Id, col.DateTimeShipped, col.Ok);
 }