I want to be able to select the columns that are displayed in my DataGridView. I.e., I have a linq query that returns an IEnumerable but I don't want to display all the properties of Policy - I want to allow the user to choose what to display. So I thought something like this might work to create a "narrower" object with only the columns I want.
IEnumerable<Policy> policies = repository.GetPolicies();
var results = from p in policies select new {
if (userPicked("PropertyOne")) PropertyOne = p.PropertyOne,
if (userPicked("PropertyTwo")) PropertyTwo = p.PropertyTwo,
...
};
dataGridView1.DataSource = results;
Is there an elegant way to do this?
Part 2: Why does this work:
IEnumerable<Policy> policies = repository.GetPolicies();
dataGridView1.DataSource = policies;
but this doesn't:
IEnumerable<Policy> policies = repository.GetPolicies();
var results = from p in policies select p;
dataGridView1.DataSource = results;
In the second case nothing is displayed in the grid.