开发者

Winforms: How to Bind a List to multiple DataGridView

I want to be able to bind a List to multiple DataGridViews such that manipulation through one of the gridviews would be propogated to all other gridviews.

List<Domain> data;

1st approach:

BindingList<Domain> list = new ..;
data.ForEach( d => { list .Add(d); } );    

grid1.DataSource = list;
grid2.DataSource = list;

This didn't work. The grids share properties other than the data.

2nd approach:

BindingList<Domain> list1 = new ..;
BindingList<Domain> list2 = new ..;

d开发者_JAVA百科ata.ForEach( d => { list1.Add(d); list2.Add(d); } );    

grid1.DataSource = list1;
grid2.DataSource = list2;

This approach works for updates. However, adds and deletes weren't propograted.

3rd approach:

BindingList<Domain> list = new ..;
data.ForEach( d => { list .Add(d); } );    

BindingSource ds1 = new BindingSource();
BindingSource ds2 = new BindingSource();
ds1.DataSource = list;
ds2.DataSource = list;

grid1.DataSource = ds1;
grid2.DataSource = ds2;

This propogates adds and deletes, however, when a new row is added to 1 view, but not yet commited, an empty row is displayed in all other grids. Seems like a new record is inserted into the List before the editing completes.

How can I properly bind multiple datagridviews to one List? (This is extremely easy in Flex.) I'd appreciate any reference to the relevant section in MDSN.


I made a little test app, just to make sure that sharing a binding source was possible the way I remembered. You can find it here (for at least 30 days).

What I found that propbably caused your problem is that all your grids probably have adding/deleting rows enabled. A new row in grid1 is displayed as a new row in grid2, but grid2 (quite unnecessarily) displays a template row below that.

I made a little main window with a read-only grid on a binding source and an edit dialog with an editable grid on the same binding source. The binding source is the only wiring between the two, no event handling to signal updates, no reassigning of datasources. Everything is synced perfectly, even the current row (because of the CurrencyManager). A new row in the dialog only shows as one empty row in the 'main' window.

Hope this helps, and is not over-simplified.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜