Solved

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

Posted on 2013-11-14
1
1,327 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now