Hello all!
I found a solution to my original question, but there's still so much I don't understand about it, I thought I'd give this forum a try. At the very least, maybe it will help someone else who got stumped like I did.
It seems so simple... binding a DataGridView to a List<T>. These are the two general problems that I kept running into:
(1) When the data in the list updated, the data on the screen did not update.
(2) When I finally got data on the screen to update, if I clicked any part of the DataGridView, the following message box popped up: "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll Additional information: Index -1 does not have a value."
Here is the code that I finally got to work. Note that this code is completely contrived. I just tried to make the simplest example binding a list to a DataGridView that I could think of. Also, I am using .NET 2.0.
-
public partial class Form1 : Form
-
{
-
private class Customer
-
{
-
private String _first;
-
private String _last;
-
-
public Customer(String first, String last)
-
{
-
_first = first;
-
_last = last;
-
}
-
-
public String first { get { return _first; } }
-
public String last { get { return _last; } }
-
}
-
private BindingList<Customer> customerList;
-
private int iCount;
-
-
public Form1()
-
{
-
InitializeComponent();
-
customerList = new BindingList<Customer>();
-
iCount = 0;
-
updateDataGridView1();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
iCount++;
-
customerList.Add(new Customer("First" + iCount.ToString(), "Last" + iCount.ToString()));
-
updateDataGridView1();
-
}
-
-
private void updateDataGridView1()
-
{
-
dataGridView1.DataSource = null;
-
dataGridView1.DataSource = customerList;
-
}
-
}
-
Of special note to anyone else having problems, this binding doesn't work unless your list class has some type "properties" member function:
-
public String first { get { return _first; } }
-
public String last { get { return _last; } }
-
Also, I had to make the following statements every time I updated the list:
-
dataGridView1.DataSource = null;
-
dataGridView1.DataSource = customerList;
-
In addition, I had to use a BindingList<T> instead of a List<T> for customerList:
-
private BindingList<Customer> customerList;
-
Finally, if I used a BindingSource between the list and the DataGridView, it made no difference at all. I had all the sample problems using the BindingSource, and if I use a BindingSource in the example above, it works fine.
So, I still have a few questions about this whole thing, but to start with, am I missing the obvious?! For instance, why can't I just bind a normal List<T> to a DataGridView and have the grid view update when the list updates? When I read examples on the web, they seem so simple... but they don't work! Also, what if your list class--"Customer" in my code--has more properties methods than you want to appear in the DataGridView? Is there any way to mask them out?
Thanks a lot for any help. This has really been kicking my butt the past couple of days. Also, thanks to the folks involved with this archived thread:
http://www.thescripts.com/forum/thread443793.html
That gave me the clues I needed to get my last problem solved.
Thanks again, and hopefully I'll be "talking" with some of you soon! :)
dave