Associated DataGridView to <T> not displaying data
This is the code I have (this is a very simple example):
public partial class Form1 : Form { List<Person> listPersons; public Form1() { InitializeComponent(); listPersons = new List<Person>(); dataGridView1.DataSource = listPersons; } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { Person p = new Person(); p.Name = textBox1.Text; listPersons.Add(p); } } } class Person { public string Name { get; set; } } When you click the button, the data is added to the list, but not displayed in the DataGridView . What am I missing?
I tried setting AutoGenerateColumns and VirtualMode to true , but this also did not solve the problem.
But if I use only BindingList<T> instead of List<T> , this will work.
Code example:
BindingList<Person> bl; public Form1() { InitializeComponent(); bl = new BindingList<Person>(); dataGridView1.DataSource = bl; } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { Person p = new Person(); p.Name = textBox1.Text; bl.Add(p); textBox1.Text = ""; textBox1.Focus(); } } But I still would like to figure out how to show the data in the DataGridView after binding it to the list.
Some time passed, and I switched jobs from working on WinForms code, which tried to associate List <t> s with DataGridViews. If I remember correctly that whatever you bind, you need to implement an IBindingList that List <T> does not. Maybe I'm wrong.
Anyway, I used the BindingListView , which was incredibly fast and easy. You just do:
List<Customer> customers = GetCustomers(); BindingListView<Customer> view = new BindingListView<Customer>(customers); dataGridView1.DataSource = view; And you're done. I did not look at the source after a few years, but I believe that it wraps List <T> with a class that implements IBindingList.
Centralize FillGrid functionality and call it when you want to update the grid
public Form1() { InitializeComponent(); listPersons = new List<Person>(); FillGrid(); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { Person p = new Person(); p.Name = textBox1.Text; listPersons.Add(p); FillGrid(); } } private void FillGrid() { dataGridView1.DataSource = listPersons; } I donβt think you can directly link the list to datagridview. Use a BindingList instead.
public partial class Form1 : Form { BindingList<Person> lstBinding; public Form1() { InitializeComponent(); lstBinding = new BindingList<Person>(); dataGridView1.DataSource = lstBinding; } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { Person p = new Person(); p.Name = textBox1.Text; lstBinding.Add(p); FillGrid(); } } private void FillGrid() { dataGridView1.DataSource = lstBinding; } } class Person { private string name; public string Name { get {return name;} set { name = value; } } } use an array to bind a datagridview, i.e.
public partial class Form1 : Form { Person[] listPersons = new Person[0]; public Form1() { InitializeComponent(); dataGridView1.DataSource = listPersons; } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length > 0) { Person p = new Person(); p.Name = textBox1.Text; Array.Resize<Person>(ref listPersons, listPersons.Length+1); listPersons[listPersons.Length-1]=p; dataGridView1.DataSource = listPersons; } } } class Person { public string Name { get; set; } }