2013-02-12 53 views
0

我有兩個關聯的實體。我通過拖放對象數據源和手動綁定到列表來創建一個dataGridView。一切正常工作與一個實體。有沒有可能通過拖放+手動填充來創建一個包含兩個實體(Zamow和ZamSkany)的dataGridView?我可以通過查看(在SQL方面)做到這一點,但在同樣的情況下,我想有其他的可能性。兩個實體在一個dataGridView中

pg = new PGEntities(); 
BindingList<Zamow> myList; 
var query = (from zam in pg.Zamow where zam.Rok == 2012 select zam).Take(100); 
MyList = new BindingList<Zamow>(query.ToList()); 
zamowBindingSource.DataSource = MyList; 

enter image description here

回答

0

是,嘗試創建Class,讓說ViewZamowAndSamSkany

public class ViewZamowAndSamSkany 
{ 
    public string Data { get; set; } 
    public string Proforma { get; set; } 
    //and Others Properties 
} 

現在,重建項目,並從Objects Data SourceViewZamowAndSamSkany然後拖放添加到您的Form作爲一個DataGridView,你可以申請linq-entites inner join

var query = (from zam in pg.Zamow 
      join skany in zam.NUMBER equals skany.NUMBER 
      where zam.Rok == 2012 
      select new ViewZamowAndSamSkany 
      { 
       Data = zam.Data, 
       Proforma = zam.Proforma 
      }).Take(100); 

MyList = new BindingList<ViewZamowAndSamSkany>(query.ToList()); 
zamowBindingSource.DataSource = MyList; 
+0

非常感謝!它像一個魅力。 – mds 2013-02-13 07:31:47

+0

不客氣':D' – spajce 2013-02-13 07:35:55