简体   繁体   English

将数据绑定到 Datagridview 的特定列 - 实体框架

[英]Bind Data to Specific Columns of Datagridview - Entity Framework

I don't know if I should take all the data or just the one I need:) I am doing it for the first time.我不知道我应该获取所有数据还是只获取我需要的数据:) 我是第一次这样做。 I want to bind this data to Specific columns我想将此数据绑定到特定列

See select见 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
});

Do this one line of code, underneath the query:在查询下方执行这一行代码:

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

The BindingSource can work with the List<anonymoustype> the query will create BindingSource 可以与查询将创建的List<anonymoustype>一起使用


Alternatively, because you're working with anonymous types you could also make an extension method to generate you a BindingList instead:或者,因为您使用的是匿名类型,所以您也可以创建一个扩展方法来为您生成一个 BindingList :

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

You can bind a datagridview to a bindingList:您可以将 datagridview 绑定到 bindingList:

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

Binding through a BindingSource gives some advantages for filtering, sorting, accessing the current item etc. It also allows you to arrange hierarchical data structures.通过 BindingSource 绑定为过滤、排序、访问当前项目等提供了一些优势。它还允许您安排分层数据结构。 If you're going to user BindingSource you should perhaps consider NOT using anonymous types, because they're compiler generated POCO classes that you don't really have any reliable access to if you wanted to dig your bindingSource's .Current object out and cast it to something you work with .如果您要使用 BindingSource 用户,您或许应该考虑不使用匿名类型,因为它们是编译器生成的 POCO 类, 如果您想挖掘您的 bindingSource 的.Current object 并将其强制转换,您实际上没有任何可靠的访问权限到你工作的东西

If instead you made your class a fully defined one in your own code, then you have:相反,如果您在自己的代码中将 class 完全定义为一个,那么您将拥有:

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

But you can work with it better:但是您可以更好地使用它:

var x = myBindingSource.Current as Whatever;

If you use anonymous types it's that as Whatever cast that you can't easily do, and you'll end up stuck with myBindingsource.Current being an object, needing either some dynamic workaround (which is not optimal when really this is a design time known class type) or a bit of a hack where you declare another anonymous type with the same order and type of parameters and rely on the compiler making them the same thing when it creates the anonymous types如果您使用匿名类型,那就是您不能轻易执行的as Whatever类型转换,并且您最终会卡在myBindingsource.Current是 object 中,需要一些dynamic解决方法(当这真的是设计时间时,这不是最佳选择已知的 class 类型)或有点hack,您声明另一个具有相同顺序和参数类型的匿名类型,并依赖编译器在创建匿名类型时使它们相同

Hi I created a small demo code.嗨,我创建了一个小演示代码。 You just need to re-write your code and It should work.你只需要重新编写你的代码,它应该可以工作。 Hope it will helps you.希望它会帮助你。

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 Data holds values from linq select. Class 数据保存来自 linq select 的值。 Also there is Dictionary which have list of desired columns including their types.还有一个字典,其中包含所需列的列表,包括它们的类型。 This Dictionary is used for creating columns which should be in DataGridView.此字典用于创建应在 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM