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

Posted on 2013-09-07
Medium Priority
Last Modified: 2013-09-08
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
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.
Question by:jackjohnson44
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39473979
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

Author Comment

ID: 39474567
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.
LVL 40
ID: 39474588
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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question