Solved

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

Posted on 2013-11-14
1
1,282 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

7 Experts available now in Live!

Get 1:1 Help Now