473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

CancelEdit in BindingSource

BD
Hi,

I have a "Cancel" button on a form to cancel any changes in the
current item with the code BindingSource1.CancelEdit but this don't
restore the older's values that had been changed. The
BindingSource.DataSource is a type of BindingList<MyClass>. What's I'm
doing wrong?

[]'s
BD
[]'s
BD
Feb 24 '07 #1
6 14935
BD wrote:
Hi,

I have a "Cancel" button on a form to cancel any changes in the
current item with the code BindingSource1.CancelEdit but this don't
restore the older's values that had been changed. The
BindingSource.DataSource is a type of BindingList<MyClass>. What's I'm
doing wrong?
Your 'MyClass' has to implement IEditableObject, which makes you
implement BeginEdit, EndEdit and CancelEdit. In the CancelEdit method,
you restore the values the fields had at the time BeginEdit was called.

Warning: this is a time waster. Grids often call BeginEdit a couple of
times, so you have to flag if you're already in edit mode. Also,
CancelEdit can also be called when you created a new row and pressed
ESC to remove the new row. This then has to make sure the new row is
removed from the original list. If you use the pre-fab BindingList, it
should take care of that, but be sure this indeed happens.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Feb 25 '07 #2
BD
On Sun, 25 Feb 2007 03:22:53 -0800, "Frans Bouma [C# MVP]"
<pe******************@xs4all.nlwrote:
>BD wrote:
>Hi,

I have a "Cancel" button on a form to cancel any changes in the
current item with the code BindingSource1.CancelEdit but this don't
restore the older's values that had been changed. The
BindingSource.DataSource is a type of BindingList<MyClass>. What's I'm
doing wrong?

Your 'MyClass' has to implement IEditableObject, which makes you
implement BeginEdit, EndEdit and CancelEdit. In the CancelEdit method,
you restore the values the fields had at the time BeginEdit was called.

Warning: this is a time waster. Grids often call BeginEdit a couple of
times, so you have to flag if you're already in edit mode. Also,
CancelEdit can also be called when you created a new row and pressed
ESC to remove the new row. This then has to make sure the new row is
removed from the original list. If you use the pre-fab BindingList, it
should take care of that, but be sure this indeed happens.

FB

Thank's for your answer. I really don't waste the time to implement
the IEditableObject in all my classes. The BindingList takes care of
call the CancelEdit internal when ESC is pressed, but since I move to
another control, even to the Cancel button used in the form, the
BindingList call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSource control to make a backup of the current item and if I
cancel the changes restore the original values from that backup. I
think this approach would resolve my problem but I have some
difficulties to restore the data into the bounds controls. Any help or
example would be very appreciate.
[]'s
BD
Feb 25 '07 #3
BD
>Thank's for your answer. I really don't waste the time to implement
the IEditableObject in all my classes. The BindingList takes care of
call the CancelEdit internal when ESC is pressed, but since I move to
another control, even to the Cancel button used in the form, the
BindingList call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSource control to make a backup of the current item and if I
cancel the changes restore the original values from that backup. I
think this approach would resolve my problem but I have some
difficulties to restore the data into the bounds controls. Any help or
example would be very appreciate.
[]'s
BD

Problem solved with FieldInfo class.

[]'s
BD
Feb 26 '07 #4

<BDwrote in message news:ki********************************@4ax.com...
>
>>Thank's for your answer. I really don't waste the time to implement
the IEditableObject in all my classes. The BindingList takes care of
call the CancelEdit internal when ESC is pressed, but since I move to
another control, even to the Cancel button used in the form, the
BindingList call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSource control to make a backup of the current item and if I
cancel the changes restore the original values from that backup. I
think this approach would resolve my problem but I have some
difficulties to restore the data into the bounds controls. Any help or
example would be very appreciate.
[]'s
BD


Problem solved with FieldInfo class.

[]'s
BD
What do you mean? I had to implement IEditable and INotifyPropertyChanged
to capture the changes to the object and be able to roll them back.

Robin S.
Feb 27 '07 #5
BD
On Tue, 27 Feb 2007 12:19:37 -0800, "RobinS" <Ro****@NoSpam.yah.none>
wrote:
>
<BDwrote in message news:ki********************************@4ax.com...
>>
>>>Thank's for your answer. I really don't waste the time to implement
the IEditableObject in all my classes. The BindingList takes care of
call the CancelEdit internal when ESC is pressed, but since I move to
another control, even to the Cancel button used in the form, the
BindingList call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSource control to make a backup of the current item and if I
cancel the changes restore the original values from that backup. I
think this approach would resolve my problem but I have some
difficulties to restore the data into the bounds controls. Any help or
example would be very appreciate.
[]'s
BD


Problem solved with FieldInfo class.

[]'s
BD

What do you mean? I had to implement IEditable and INotifyPropertyChanged
to capture the changes to the object and be able to roll them back.

Robin S.

I did not intend to implement that in all my objects and I made two
generic function to backup and restore the data using Stack<byte[]>,
HybridDictionary, FieldInfo[], MemoryStream and BinaryFormatter.

private Stack<byte[]_backup;

private void BackupData()
{
if (_backup == null)
{
_backup = new Stack<byte[]>();
object source = BindingSource.Current;
Type sourceType = source.GetType();

HybridDictionary state = new HybridDictionary();

FieldInfo[] fields;
do
{
// get the list of fields in this type
fields = sourceType.GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Public);

foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.DeclaringType == sourceType)
{

object value = field.GetValue(source);

if(sourceType.IsAssignableFrom(field.FieldType))
{
// make sure the variable has a value
if (value == null)
{
// variable has no value - store that fact
state.Add(field.DeclaringType.Name + "!" +
field.Name, null);
}
else
{
// this is a child object, cascade
the call
((this)value).BackupData();
}
}
else
{
// this is a normal field, simply trap the value
state.Add(field.DeclaringType.Name +
"!" + field.Name, value);
}

}
}
sourceType = sourceType.BaseType;
} while (sourceType != null);

// serialize the state and stack it
using (MemoryStream buffer = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(buffer, state);
_backup.Push(buffer.ToArray());
}

}
}

private void RestoreData()
{

// if we are a child object we might be asked to
// undo below the level where stacked states,
// so just do nothing in that case
if (_backup != null)
{
HybridDictionary state;
using (MemoryStream buffer = new
MemoryStream(_backup.Pop()))
{
buffer.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
state =
(HybridDictionary)formatter.Deserialize(buffer);
}

object source = BindingSource.Current;
Type sourceType = source.GetType();

FieldInfo[] fields;

do
{
// get the list of fields in this type
fields = sourceType.GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Public);
foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.DeclaringType == sourceType)
{
// the field is undoable, so restore its
value
object value = field.GetValue(source);

if(sourceType.IsAssignableFrom(field.FieldType))
{
// this is a child object
// see if the previous value was empty
if(state.Contains(field.DeclaringType.Name + "!" + field.Name))
{
// previous value was empty -
restore to empty
field.SetValue(source, null);
}
else
{
// make sure the variable has a
value
if (value != null)
{
// this is a child object,
cascade the call.
((this)value).RestoreData();
}
}
}
else
{
// this is a regular field, restore
its value
field.SetValue(source,
state[field.DeclaringType.Name + "!" + field.Name]);
}

}
}
sourceType = sourceType.BaseType;
} while (sourceType != null);

BindingSource.ResetCurrentItem();

_backup = null;

}
}
[]'s
BD
Mar 1 '07 #6

<BDwrote in message news:iu********************************@4ax.com...
On Tue, 27 Feb 2007 12:19:37 -0800, "RobinS" <Ro****@NoSpam.yah.none>
wrote:
>>
<BDwrote in message news:ki********************************@4ax.com...
>>>
Thank's for your answer. I really don't waste the time to implement
the IEditableObject in all my classes. The BindingList takes care of
call the CancelEdit internal when ESC is pressed, but since I move to
another control, even to the Cancel button used in the form, the
BindingList call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSource control to make a backup of the current item and if I
cancel the changes restore the original values from that backup. I
think this approach would resolve my problem but I have some
difficulties to restore the data into the bounds controls. Any help or
example would be very appreciate.
[]'s
BD
Problem solved with FieldInfo class.

[]'s
BD

What do you mean? I had to implement IEditable and INotifyPropertyChanged
to capture the changes to the object and be able to roll them back.

Robin S.


I did not intend to implement that in all my objects and I made two
generic function to backup and restore the data using Stack<byte[]>,
HybridDictionary, FieldInfo[], MemoryStream and BinaryFormatter.

private Stack<byte[]_backup;

private void BackupData()
{
if (_backup == null)
{
_backup = new Stack<byte[]>();
object source = BindingSource.Current;
Type sourceType = source.GetType();

HybridDictionary state = new HybridDictionary();

FieldInfo[] fields;
do
{
// get the list of fields in this type
fields = sourceType.GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Public);

foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.DeclaringType == sourceType)
{

object value = field.GetValue(source);

if(sourceType.IsAssignableFrom(field.FieldType))
{
// make sure the variable has a value
if (value == null)
{
// variable has no value - store that fact
state.Add(field.DeclaringType.Name + "!" +
field.Name, null);
}
else
{
// this is a child object, cascade
the call
((this)value).BackupData();
}
}
else
{
// this is a normal field, simply trap the value
state.Add(field.DeclaringType.Name +
"!" + field.Name, value);
}

}
}
sourceType = sourceType.BaseType;
} while (sourceType != null);

// serialize the state and stack it
using (MemoryStream buffer = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(buffer, state);
_backup.Push(buffer.ToArray());
}

}
}

private void RestoreData()
{

// if we are a child object we might be asked to
// undo below the level where stacked states,
// so just do nothing in that case
if (_backup != null)
{
HybridDictionary state;
using (MemoryStream buffer = new
MemoryStream(_backup.Pop()))
{
buffer.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
state =
(HybridDictionary)formatter.Deserialize(buffer);
}

object source = BindingSource.Current;
Type sourceType = source.GetType();

FieldInfo[] fields;

do
{
// get the list of fields in this type
fields = sourceType.GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Public);
foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.DeclaringType == sourceType)
{
// the field is undoable, so restore its
value
object value = field.GetValue(source);

if(sourceType.IsAssignableFrom(field.FieldType))
{
// this is a child object
// see if the previous value was empty
if(state.Contains(field.DeclaringType.Name + "!" + field.Name))
{
// previous value was empty -
restore to empty
field.SetValue(source, null);
}
else
{
// make sure the variable has a
value
if (value != null)
{
// this is a child object,
cascade the call.
((this)value).RestoreData();
}
}
}
else
{
// this is a regular field, restore
its value
field.SetValue(source,
state[field.DeclaringType.Name + "!" + field.Name]);
}

}
}
sourceType = sourceType.BaseType;
} while (sourceType != null);

BindingSource.ResetCurrentItem();

_backup = null;

}
}
[]'s
BD
Cool. Thanks for displaying your solution.

Robin S.
Mar 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TN | last post by:
I just don't get the BindingSource class when it is bound to a class. Consider this code snip: ..... Private bSource As New BindingSource() Private dgv As New DataGridView() Public Sub New()...
1
by: Leonardo | last post by:
Hi. I'm trying to build my first application with database access using VB 2005. I'm a VB 6 programmer and learning everything again has been challenging. I managed to write a code using some tips...
0
by: Geoff | last post by:
Hi folks Calling BindingSource.ResetCurrentItem() is changing the BindingSource.Position in a way I don't understand. If I'm understanding the docs correctly, ResetCurrentItem() should simply...
4
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I...
0
by: BD | last post by:
Hi, I have a "Cancel" button on a form to cancel any changes in the current item with the code BindingSource1.CancelEdit() but this don't restore the older's values that had been changed. The...
7
by: Mike | last post by:
i have a small difficulties with BindingSource and dataGridView bind db has properly opened and bind doesn't works. Unfortunately I didn't find any good example how to connect MS Access with...
2
by: jehugaleahsa | last post by:
Hello: I'm working on improving some of our Windows Forms. I have created two user controls that I want to bind to the same BindingSource. I have an overview control that is used just to...
5
by: jehugaleahsa | last post by:
Hello: I am sure this question comes up a lot. I need to disable the controls on my Windows forms so that when the BindingSource is empty some the controls bound to it will be disabled. This...
2
by: TamusJRoyce | last post by:
I am working on a group project where I do not have access to a BindingSource that is being passed through a function. The BindingSource's DataSource is set to a custom IBindingListView Collection....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.