2012-12-05 72 views
-1

我有兩個表,A和B.表B從表A接收外鍵,A.id。在DataGridView中,我想顯示A.FirstName,而不是在表B中顯示錶A的ID。這可能嗎?DataGridView和外鍵

回答

0
using System.Linq; 

... 

//Initialization, please ignore it, as you have data tables A and B populated from the database 
/********************/ 
DataTable A, B; 
A = new DataTable(); 
B = new DataTable(); 
A.Columns.Add("Id",typeof(int)); 
A.Columns.Add("ForeignId",typeof(int)); 
B.Columns.Add("Id",typeof(int)); 
B.Columns.Add("Name", typeof(string)); 

A.Rows.Add(1,1); 
B.Rows.Add(1,"First"); 
/********************/ 

var aEnum = A.AsEnumerable(); 
var bEnum = B.AsEnumerable(); 

dataGridView1.DataSource = (from a in aEnum 
          join b in bEnum on a["ForeignId"] equals b["Id"] 
          select new 
          { 
           Id=a["Id"], 
           ForeignId = a["ForeignId"], 
           ForeignName = b["Name"] 
          }).ToList();