Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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