?
Solved

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

Posted on 2013-11-14
1
Medium Priority
?
1,501 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

764 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