简体   繁体   中英

DataGridView - adding to DataSource doesn't reflect changes in the control

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.

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 :)

添加到数据源列表后,您必须重新绑定,因此请重做:

dataGridView1.DataSource = document.m_statement.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM