Groups | Blog | Home
all groups > dotnet windows forms databinding > october 2005 >

dotnet windows forms databinding : Confirm changes on BindingNavigator


Jeronimo Bertran
10/17/2005 6:26:08 PM
Hello,

The default implementation of the BindingNavigator control saves changes to
the datasource when the user navigates to a different record. I want to
let the user decide if he wants to save changes when the record has been
modified. Is there some event that can be handled before a record is saved
that will let me know that the record is dirty and will be saved and that
will allow me to cancel the operation?

thanks

v-jetan NO[at]SPAM online.microsoft.com (
10/18/2005 7:30:03 AM
Hi Jeronimo,

Thanks for your post.

Yes, in .Net2.0, we can cancel the databinding now. Whenever we move to
next record in BindingNavigator, it will first validate the original
binding value for the bound control. So the TextBox's Validating event will
fire for it.

We can store the original binding value in a private field, then compare
the new TextBox value with the original cached value to determine if the
content is modified. And do canceling for it. The code snippet demonstrate
this:
private void LoadData()
{
DataSet set = new DataSet();
DataTable dt = new DataTable();
set.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Columns.Add("column2", typeof(string));

bindingSource1.DataSource = set;
bindingSource1.DataMember = dt.TableName;
textBox1.DataBindings.Add("Text", bindingSource1, "column1");
textBox2.DataBindings.Add("Text", bindingSource1, "column2");



this.textBox1.DataBindings[0].Parse += new
ConvertEventHandler(Form1_Parse);
this.textBox1.DataBindings[0].Format += new
ConvertEventHandler(Form1_Format);
// Set the DataSource to the DataSet, and the DataMember
// to state.

for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i.ToString();
dr["column2"] = "item"+i.ToString();
dt.Rows.Add(dr);
}
}

private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}

private string orig_val = string.Empty;
void Form1_Format(object sender, ConvertEventArgs e)
{
orig_val = e.Value.ToString();
}

void Form1_Parse(object sender, ConvertEventArgs e)
{
}

private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (this.textBox1.Text!=orig_val)
{
MessageBox.Show("cancel modifying");
e.Cancel = true;
this.textBox1.Text = orig_val;
}
}
Hope this helps.
============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
v-jetan NO[at]SPAM online.microsoft.com (
10/24/2005 12:00:00 AM
Hi Jeronimo,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Jeronimo Bertran
12/13/2005 10:32:57 PM
Jeffrey,

Sorry I took so long to get back. I understood you sample but what I
am trying to do is give the user the option to cancel an update when
moving out of a record. When a user tries to move out of a dirty
record, he is given three optons, Save and move out, Don´t save and move
out, or Cancel (don´t move out).

This is what I was doing in .net 1.1. Whenever the user is about to
navigate out of a record I call SaveRecord


private bool SaveRecord(bool prompt)
{
if (bindingManagerBase == null)
return false;

try
{
if (Dirty && prompt && ! automaticSave)
{
DialogResult result = MessageBox.Show("The record has
changed.\nDo you want to save changes to the record?", "Save Changes",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
return false;
if (result == DialogResult.No)
{
bindingManagerBase.CancelCurrentEdit();
return true;
}
}

System.ComponentModel.CancelEventArgs cea = new
CancelEventArgs(false);
if (Dirty && (BeforeSave != null))
BeforeSave (this, cea);

if (cea.Cancel)
return false;

bindingManagerBase.EndCurrentEdit();

if (Dirty && (RecordSaved != null))
RecordSaved(this, EventArgs.Empty);

// Call the DatasetChanged event
if (Dirty && (navigatorUpdate == DataNavigatorUpdate.always)
&& (DatasetChanged != null))
DatasetChanged(this, EventArgs.Empty);
}
catch (Exception e)
{
MessageBox.Show("The record could not be saved.\n" +
e.Message, "Cannot Save Record", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return false;
}

return true;
}


Your sample seems to produce a message for each bound field....

What I am currently trying to do is similar to what I was doing in
version 1.1 using bindingSource.CancelEdit and BindingSource.EndEdit. I
think that the only thing I am missing is the ability to override the
default behaviour when one of the navigation buttons is pressed...

According to the documentation:

"Override AddStandardItems in derived classes to define additional or
alternative tool strip items. "

I defined

public override void AddStandardItems()
{
base.AddStandardItems();

}
but it never gets called.

Thanks

v-jetan NO[at]SPAM online.microsoft.com (
12/29/2005 7:01:10 AM
Hi Jeronimo,

If you pass true to the constructor of BindingNavigator, it will call
BindingNavigator.AddStandardItems method.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button