Synchronizing 2 grids via the CurrencyManager

P

Pete Davis

I have written a custom databound grid control that we've been using
successfully for months, but one of our developers has just run into a
problem that I can't figure out.

The code for setting it up is as follows:

DataSet dataSet = new DataSet( "TestSet" );

DataTable tableParent = dataSet.Tables.Add( "TableParent" );
tableParent.Columns.Add( "ID", typeof( Int32 ) );
tableParent.Columns[ "ID" ].AutoIncrement = true;
tableParent.Columns[ "ID" ].Unique = true;
tableParent.Columns.Add( "Col1", typeof(Int32) );
tableParent.Columns.Add( "Col2", typeof(Int32) );

DataTable tableChild = dataSet.Tables.Add( "TableChild" );
tableChild.Columns.Add( "ID", typeof( Int32 ) );
tableChild.Columns.Add( "Col1", typeof(Int32) );
tableChild.Columns.Add( "Col2", typeof(Int32) );

dataSet.Relations.Add( "Child2Parent", tableParent.Columns[ "ID" ],
tableChild.Columns[ "ID" ] ).Nested = true;

gridParent.DataSource = dataSet;
gridParent.DataMember = "TableParent";
gridChild.DataSource = dataSet;
gridChild.DataMember = "TableParent.Child2Parent";

The way it is supposed to work (and does work with the MS DataGrid) is that
when you select a row in the parent table, it should set the data in the
child table to all children of the selected parent.

In my grid, when the current cell changes, I set the position in the
CurrencyManager to the new position. My understanding (and apparently a
false one) was that the CurrencyManager would update the position in the
underlying dataset which in turn would then inform all CurrencyManagers
bound to it. Apparently that's not the case.

The parent grid receives the CurrencyManager.PositionChanged event but the
child grid receives no events from its CurrencyManager.

How am I supposed to propagate an update to the child grid?

Thanks.

Pete
 
P

Pete Davis

Okay, after digging through DataGrid, BindingContext, CurrencyManager and
RelatedCurrencyManager, I have a much better idea of how it's supposed to
operate, but I still haven't solved the problem.

Both the parent and child grids are getting RelatedCurrencyManager objects
from the BindingContext because they both have DataMember set, and as far as
I can tell, this is correct. The parent grid's RelatedCurrencyManager is the
parent of the child grid's RelatedCurrencyManager.

The child RelatedCurrencyManager is subscribing to the CurrentChanged of the
parent. Now, I know the parent CurrentChanged is getting called, because I
subscribe to it in my grid.

In RelatedCurrencyManager.ParentManager_CurrentChanged, a call is made to
this.SetDataSource() which should trigger calls to ItemChanged and
PositionChanged in the child grid's RelatedCurrencyManager, which the child
grid subscribes to, but these aren't getting called.

I'm just stumped.

Pete
Pete Davis said:
I have written a custom databound grid control that we've been using
successfully for months, but one of our developers has just run into a
problem that I can't figure out.

The code for setting it up is as follows:

DataSet dataSet = new DataSet( "TestSet" );

DataTable tableParent = dataSet.Tables.Add( "TableParent" );
tableParent.Columns.Add( "ID", typeof( Int32 ) );
tableParent.Columns[ "ID" ].AutoIncrement = true;
tableParent.Columns[ "ID" ].Unique = true;
tableParent.Columns.Add( "Col1", typeof(Int32) );
tableParent.Columns.Add( "Col2", typeof(Int32) );

DataTable tableChild = dataSet.Tables.Add( "TableChild" );
tableChild.Columns.Add( "ID", typeof( Int32 ) );
tableChild.Columns.Add( "Col1", typeof(Int32) );
tableChild.Columns.Add( "Col2", typeof(Int32) );

dataSet.Relations.Add( "Child2Parent", tableParent.Columns[ "ID" ],
tableChild.Columns[ "ID" ] ).Nested = true;

gridParent.DataSource = dataSet;
gridParent.DataMember = "TableParent";
gridChild.DataSource = dataSet;
gridChild.DataMember = "TableParent.Child2Parent";

The way it is supposed to work (and does work with the MS DataGrid) is that
when you select a row in the parent table, it should set the data in the
child table to all children of the selected parent.

In my grid, when the current cell changes, I set the position in the
CurrencyManager to the new position. My understanding (and apparently a
false one) was that the CurrencyManager would update the position in the
underlying dataset which in turn would then inform all CurrencyManagers
bound to it. Apparently that's not the case.

The parent grid receives the CurrencyManager.PositionChanged event but the
child grid receives no events from its CurrencyManager.

How am I supposed to propagate an update to the child grid?

Thanks.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top