-1

我有两个表,A 和 B。表 B 接收来自表 A 的外键,A.id. 在DataGridView中,我不想在表 B 中显示表 A 的 id,而是显示A.FirstName. 这可能吗?

4

1 回答 1

-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();
于 2012-12-05T10:38:48.657 回答