Windows Forms DataBinding Positions problem and solution

Windows Forms DataBinding Positions problem and solution

Old forum URL: forums.lhotka.net/forums/t/7507.aspx


CorsairX posted on Tuesday, August 25, 2009

Hello,

i am new user to CSLA.NET. For the moment i am using it in Windows Forms Applications.

I found a problem in BindingSourceNode and helper mechanism. After i Save the root object the positions of all child bindingsources are reset as the way to do it is to rebind the ui as i understood.

I tried first to extend the BindingSourceNode class but i couldn;t have access to _Source and other nice properties, so that i added some methods to the one from the framework. i dont know if this is allowed, so that i post here them for the responsibles of this project to take a look and give an opinion about them.

Basically i add 2 public methods UserBeginEdit and UserEndEdit that saves/restores positions of all the tree of bindingsources. UserBeginEdit must be called before MyObject.Save() and the other one after.
The additions are :

private int _currentPosition = -1;
public int CurrentPosition {
get { return _currentPosition; }
set { _currentPosition = value; }
}

private void SavePositions( BindingSourceNode bs ) {

bs.CurrentPosition = bs.Source.Position;

if ( bs.Children.Count > 0 ) {
foreach ( BindingSourceNode child in bs.Children ) {
SavePositions( child );
} //EndFor
} EndIf
}EdnMethod

private void RestorePositions( BindingSourceNode bs ) {

bs.Source.Position = bs.CurrentPosition;
if ( bs.Children.Count > 0 ) {

foreach ( BindingSourceNode child in bs.Children ) {
RestorePositions( child );
} // EndFor
} //EndIf
} //EndMethod

public void UserBeginEdit( ) {
SavePositions( this );
}

public void UserEndEdit( ) {
RestorePositions( this );
}

I am currently working to have them also working when the user adds/deletes from a sorted List.

Thank you and i can hardly wait for your comments positive or negative about this.
Have a nice day.

RockfordLhotka replied on Tuesday, August 25, 2009

This seems like a good idea overall. I wonder if it would be better to raise events so the UI developer could store/restore the positions and do other things if they needed?

I also wonder what would happen if the new object graph was different - in particular suppose the new object graph had fewer items, so the positions were no longer valid - would that cause exceptions?

Copyright (c) Marimer LLC