我有两个表,A和B.表B从表A A.id
接收外键。在DataGridView
中,我想显示A.FirstName
,而不是在表B中显示表A的ID。这可能吗?
答案 0 :(得分:-1)
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();