views:

401

answers:

2

I have a DataGridView with its DataSource set to a List. When I Add to that list, the change isn't picked up by the DataGridView. The exact code for initialisation is:

dataGridView1.DataSource = document.m_statement.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN;
dataGridView1.AllowUserToAddRows = true;
dataGridView1.AllowUserToDeleteRows = true;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.AllowUserToResizeColumns = true;
dataGridView1.AllowUserToResizeRows = false;

And the exact code for updating the datasource is:

SimpleOfx.OFXBANKMSGSRSV1STMTTRNRSSTMTRSBANKTRANLISTSTMTTRN transaction = new SimpleOfx.OFXBANKMSGSRSV1STMTTRNRSSTMTRSBANKTRANLISTSTMTTRN();
document.m_statement.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN.Add(transaction);

As you can probably tell, the class names have been generated using xsd.exe, SimpleOfx is the namespace of the generated class, STMTTRN is a list, document is an instance of a class that contains a deserialised XML file, and m_statement is that file.

Any idea what I have to do to get the control to reflect changes made to the DataSource after it has been set? I've tried setting it again but that doesn't work. I've tried 'refresh' and all sorts of other methods that sound hopeful but they haven't worked either. Thanks in advance :)

EDIT: A little bit more information; I can break into the code and have a look at both my 'document' member and the 'DataSource' property of dataGridView1; both of them have the correct number of elements, including some default-constructed ones at the end, but the control still doesn't draw these extra rows. The DataGridView's DataSource does appear to know of the new contents of the list, but isn't drawing them.

A: 

You'll have to rebind after adding to the datasource list, so redo:

dataGridView1.DataSource = document.m_statement.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN;
Jan Jongboom
I've tried exactly that, and it didn't work.
Ben Hymers
+3  A: 

I solved this by changing STMTTRN from a List to a BindingList - List seems to have some very odd behaviour when used as a DataSource (see my other recent question, which is solved in the same way).

It's in generated code, but I'd already changed it from an Array to a List so BindingList is barely any extra trouble :)

Ben Hymers