Solved

How do I bind a List<T> to a DataGridView control with predefined columns .Net winforms?

Posted on 2013-11-14
1
1,453 Views
Last Modified: 2013-11-24
Hi:

I'm building a .net winform that contains a DataGridView.
I would like like to use a List<SomeClass> as the datasource for the DataGridView.

My columns in the DataGridView contain columns defined as  both:
DataGridViewTextBoxColumn and DataGridViewComboBoxColumn

I named all my columns in the DataGridView to have the same names as the fields in the definition of <SomeClass>.

When I assign the datasource, the DataGridView adds the correct number of rows, but adds new columns for each of the fields in my class : <SomeClass>. I tried setting AutoGenerateColumns to false. That prevents the new columns from getting created, but does not help me with my binding issues.

I've tried using BindingList and BindingSource, but that still does not work

How do I get this to work?

public class SomeClass
{
        public String Field1{ get; set; }
        public String Field2 { get; set; }
        public String Field3 { get; set; }
}

List<SomeClass> dgvList;

dataGridView1.AutoGenerateColumns = false;
var bindingList = new BindingList<SomeClass>(dgvList);
var source = new BindingSource(bindingList, null);
dataGridView1.DataSource = source;

Open in new window

0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
Ironhoofs earned 500 total points
ID: 39650587
Because AutoGenerateColumns is set to false, you still need to bind the DataViewColumn to a property in your class.
dataGridView1.AutoGenerateColumns = false;

Open in new window

Try this code:

public class SomeClass {
	public SomeClass(string fld1, string fld2, string fld3) {
		Field1 = fld1;
		Field2 = fld2;
		Field3 = fld3;
	}

	public String Field1 { get; set; }
	public String Field2 { get; set; }
	public String Field3 { get; set; }
}

List<SomeClass> dgvList = new List<SomeClass>();

for (int Row = 1; Row < 5; Row++) {
	dgvList.Add(new SomeClass("Row " + Row.ToString() + ", Field 1", "Row " + Row.ToString() + ", Field 2", "Row " + Row.ToString() + ", Field 3"));
}

dataGridView1.AutoGenerateColumns = false;
var bindingList = new BindingList<SomeClass>(dgvList);
var source = new BindingSource(bindingList, null);
dataGridView1.DataSource = source;
dataGridView1.Columns[0].DataPropertyName = "Field1";
dataGridView1.Columns[1].DataPropertyName = "Field2";
dataGridView1.Columns[2].DataPropertyName = "Field3";

Open in new window

0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

688 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