我有以下代碼來填充的DataGridViewLINQ到的DataGridView和回KeyValuePair(或類似)
var results = from loc in dtLocations.AsEnumerable()
join con in dtContacts.AsEnumerable() on (int)loc["contactid"] equals (int)con["id"]
select new
{
id = con["id"],
mpoc = loc["mpoc"],
directno = loc["directno"],
extension = loc["extension"],
faxno = loc["faxno"],
billing = con["billing"],
fullname = con["fullname"],
mobno = con["mobno"],
email = con["email"]
};
dgv.AutoGenerateColumns = false;
dgv.DataSource = results.ToList<object>();
,但我不能在當我點擊細胞
private void dgvLocations_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
????? vals = ((List<object>)dgv.DataSource)[e.RowIndex];
object id = vals.id; //errors of course
}
我讀回可以在Visual Studio的Watcher面板中看到它可以確定列表中的元素,但我無法弄清楚需要設置val以便再次讀取它們的類型:(
它是一個匿名類型。 *不能*以靜態類型表示(除了引入時)。使用命名(非匿名)類型或「動態」或(甚至是ickier,「對象」和顯式反射)。 – 2012-09-22 22:22:08