473,687 Members | 2,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.D ataSource is a type of BindingList<MyC lass>. What's I'm
doing wrong?

[]'s
BD
[]'s
BD
Feb 24 '07 #1
6 14981
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.D ataSource is a type of BindingList<MyC lass>. 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.n lwrote:
>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<MyC lass>. 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
BindingSourc e 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.c om...
>
>>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
BindingSour ce 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
difficultie s 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 INotifyProperty Changed
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.c om...
>>
>>>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
BindingLis t call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSourc e 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
difficulti es 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 INotifyProperty Changed
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[]>,
HybridDictionar y, FieldInfo[], MemoryStream and BinaryFormatter .

private Stack<byte[]_backup;

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

HybridDictionar y state = new HybridDictionar y();

FieldInfo[] fields;
do
{
// get the list of fields in this type
fields = sourceType.GetF ields(
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Pu blic);

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

object value = field.GetValue( source);

if(sourceType.I sAssignableFrom (field.FieldTyp e))
{
// 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).B ackupData();
}
}
else
{
// this is a normal field, simply trap the value
state.Add(field .DeclaringType. Name +
"!" + field.Name, value);
}

}
}
sourceType = sourceType.Base Type;
} while (sourceType != null);

// serialize the state and stack it
using (MemoryStream buffer = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter ();
formatter.Seria lize(buffer, state);
_backup.Push(bu ffer.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)
{
HybridDictionar y state;
using (MemoryStream buffer = new
MemoryStream(_b ackup.Pop()))
{
buffer.Position = 0;
BinaryFormatter formatter = new BinaryFormatter ();
state =
(HybridDictiona ry)formatter.De serialize(buffe r);
}

object source = BindingSource.C urrent;
Type sourceType = source.GetType( );

FieldInfo[] fields;

do
{
// get the list of fields in this type
fields = sourceType.GetF ields(
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Pu blic);
foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.Declarin gType == sourceType)
{
// the field is undoable, so restore its
value
object value = field.GetValue( source);

if(sourceType.I sAssignableFrom (field.FieldTyp e))
{
// this is a child object
// see if the previous value was empty
if(state.Contai ns(field.Declar ingType.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).R estoreData();
}
}
}
else
{
// this is a regular field, restore
its value
field.SetValue( source,
state[field.Declaring Type.Name + "!" + field.Name]);
}

}
}
sourceType = sourceType.Base Type;
} while (sourceType != null);

BindingSource.R esetCurrentItem ();

_backup = null;

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

<BDwrote in message news:iu******** *************** *********@4ax.c om...
On Tue, 27 Feb 2007 12:19:37 -0800, "RobinS" <Ro****@NoSpam. yah.none>
wrote:
>>
<BDwrote in message news:ki******** *************** *********@4ax.c om...
>>>
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
BindingLi st call the EndEdit internal and I can't undo the properties
changed to the original value. I think I have to extend the
BindingSour ce 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
difficultie s 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 INotifyProperty Changed
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[]>,
HybridDictionar y, FieldInfo[], MemoryStream and BinaryFormatter .

private Stack<byte[]_backup;

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

HybridDictionar y state = new HybridDictionar y();

FieldInfo[] fields;
do
{
// get the list of fields in this type
fields = sourceType.GetF ields(
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Pu blic);

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

object value = field.GetValue( source);

if(sourceType.I sAssignableFrom (field.FieldTyp e))
{
// 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).B ackupData();
}
}
else
{
// this is a normal field, simply trap the value
state.Add(field .DeclaringType. Name +
"!" + field.Name, value);
}

}
}
sourceType = sourceType.Base Type;
} while (sourceType != null);

// serialize the state and stack it
using (MemoryStream buffer = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter ();
formatter.Seria lize(buffer, state);
_backup.Push(bu ffer.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)
{
HybridDictionar y state;
using (MemoryStream buffer = new
MemoryStream(_b ackup.Pop()))
{
buffer.Position = 0;
BinaryFormatter formatter = new BinaryFormatter ();
state =
(HybridDictiona ry)formatter.De serialize(buffe r);
}

object source = BindingSource.C urrent;
Type sourceType = source.GetType( );

FieldInfo[] fields;

do
{
// get the list of fields in this type
fields = sourceType.GetF ields(
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.Pu blic);
foreach (FieldInfo field in fields)
{
// make sure we process only our variables
if (field.Declarin gType == sourceType)
{
// the field is undoable, so restore its
value
object value = field.GetValue( source);

if(sourceType.I sAssignableFrom (field.FieldTyp e))
{
// this is a child object
// see if the previous value was empty
if(state.Contai ns(field.Declar ingType.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).R estoreData();
}
}
}
else
{
// this is a regular field, restore
its value
field.SetValue( source,
state[field.Declaring Type.Name + "!" + field.Name]);
}

}
}
sourceType = sourceType.Base Type;
} while (sourceType != null);

BindingSource.R esetCurrentItem ();

_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
4535
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() InitializeComponent()
1
12163
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 from a book I recently bought. The navigation works fine. My problem starts when it comes to adding new registers. I have three tables: clients, addresses and telephones, so the client can have multiple address and telephones. The client data is...
0
4149
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 trigger an event which causes controls to refresh with the values of the current item with the index BindingSource.Position. This is not what I'm seeing.
4
18706
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 put DataGridView and BindingSource, and connected them (BindingSource of course is also connected with DataSet). Next on the separated thread I'm reading data from DB and load them do DataSet (more preciselly, readed data I've saved as XML and...
0
1724
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 BindingSource.DataSource is a type of BindingList<MyClass>. What's I'm doing wrong? 's BD
7
13447
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 dataGridView and BindSource class. Maybe something like this: DataTable dtResult = new DataTable(); dtResult = GetData(); // this line returns a error GetData doesn't exists!
2
4504
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 identify a customer. Then there is a details view that shows all their specifics. I wanted to keep navigation outside of my view. There is also a
5
4934
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 will make it clear to the user that they have to create a new item first before they start working. This needs to be an easy process because there are many, many forms.
2
4662
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. When I try to set IsNew on the BindingSource having the custom IBindingListView, the BindingSource IsNew remaines false. I have been able to do a test with my own IBindingListView being set on the BindingSource, and if I set IsNew before I set...
0
8527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8781
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1947
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.