I have the following code to populate a DataGridView
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>();
But I cant read it back in when I click a cell
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
}
I can see in the Watcher panel of Visual Studio that it can determine the elements in the List, but I cant figure out the type that I need to set vals to in order to read them back again 🙁

It (
new { .. }) introduces an anonymous type. Anonymous types cannot be specified by a statically-known (“compile time”) name.Use a named (non-anonymous) type (where
MyRowis an already defined class with the required properties):Also the
dynamictype can be used with C#/.NET4+. Or, even ickier,objectand explicit reflection. (Note thatdynamic-typed expressions effectively handle the icky reflection automatically; just as withobject, static typing information is still lost.)