Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

datagridview how do I set columns and settings after binding to a list of objects?

I have a ton of dataviews bound to lists of objects.

I don't need all columns so I do things like
var data = (from item in AllItems
select new
{
Name,
Address,
Phone});
grid.datasource = data;

This works well for testing purposes but I need to make the grids look a little nicer.  For example, some grids could have ID columns I don't want shown.

I am looking for a way to make a central area to define my grids so I can reuse the layout.  For example, I could have a
public static void CreateGridItems(DataGridView dgv)
....

I'm wondering what is the best way to setup a grid if I want to through code?  Is my example above good? Is there some pattern I should follow, or a better way to do this?  Since I will want to set column width, hide columns etc, so I am not just looking for a subclass of my main object.  The goal is to setup the grid, not just filter out columns.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jackjohnson44
jackjohnson44

ASKER

Thanks, so would the process be to bind like above then set the columns according to their title?

I was thinking I needed to setup the grid first then bind, or use an object in the middle like a dataview or something.

Would a dataview solution be better?  I haven't touched those in a while, and can't remember why I used them before.
You bind first. This creates the columns with default values (header text is the name of the property or field), and you can change them afterward.

A DataView is the equivalent of a View in a database. It is simply a way of filtering and sorting a DataTable already created in memory. Since you do not have a DataTable, and since you seem to display all your data, a DataView would be useless.