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.
C#.NET Programming

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jacques Bourgeois (James Burger)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
jackjohnson44

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.
Jacques Bourgeois (James Burger)

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck