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.
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Simply reference a Column by either its name or its position, and play with its properties. Here is a little sampler for a column that displays the Product_ID field in a grid named dgvInventory:

With dgvInventory.Columns("Product_ID") 'or dgvInventory.Columns(1)
     .HeaderText = "ID"
     .DefaultCellStyle.ForeColor = Color.Blue
     .DefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Bold Or FontStyle.Italic)
     .ReadOnly = True
End With
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jackjohnson44Author Commented:
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.
0
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.