PC Review


Reply
Thread Tools Rate Thread

BindingSource.EndEdit doesn't work with FormClosing

 
 
Evan M.
Guest
Posts: n/a
 
      3rd Dec 2007
I have an appication, where a NumericUpDown control has a databinding
to a BindingSource, which has it's data source set to something else.
I also have my form's Formclosing event wired up to detect if any
changes have been made, and if so to prompt the user asking them if
they want to save. What I am finding that when I call the
BindingSource.EndEdit() method within my FormClosing event handler,
the changes aren't being propigated to my data source (and hence the
changes aren't being detected). Here is a sample application that
shows this behavior:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EndEditExample {

static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main () {
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new Form1 ());
}
}

class ExampleClass {
private int exampleVal = 0;

public int ExampleVal {
get { return exampleVal; }
set { exampleVal = value; }
}
}

public partial class Form1 : Form {
private ExampleClass exValue;

#region Form1.Designer.cs code
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose (bool disposing) {
if (disposing && (components != null)) {
components.Dispose ();
}
base.Dispose (disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent () {
this.components = new System.ComponentModel.Container ();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown ();
this.bindingSource1 = new System.Windows.Forms.BindingSource
(this.components);
((System.ComponentModel.ISupportInitialize)
(this.numericUpDown1)).BeginInit ();
((System.ComponentModel.ISupportInitialize)
(this.bindingSource1)).BeginInit ();
this.SuspendLayout ();
//
// numericUpDown1
//
this.numericUpDown1.DataBindings.Add (new
System.Windows.Forms.Binding ("Value", this.bindingSource1,
"ExampleVal", true));
this.numericUpDown1.Location = new System.Drawing.Point (13, 13);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size (120, 20);
this.numericUpDown1.TabIndex = 0;
//
// bindingSource1
//
this.bindingSource1.DataSource = typeof
(EndEditExample.ExampleClass);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF (6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size (292, 266);
this.Controls.Add (this.numericUpDown1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new
System.Windows.Forms.FormClosingEventHandler (this.Form1_FormClosing);
this.Load += new System.EventHandler (this.Form1_Load);
((System.ComponentModel.ISupportInitialize)
(this.numericUpDown1)).EndInit ();
((System.ComponentModel.ISupportInitialize)
(this.bindingSource1)).EndInit ();
this.ResumeLayout (false);

}

#endregion

private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.BindingSource bindingSource1;
#endregion

public Form1 () {
InitializeComponent ();
}

private void Form1_FormClosing (object sender, FormClosingEventArgs
e) {
bindingSource1.EndEdit ();

MessageBox.Show (String.Format ("Numeric Value: {0}\nObject Value:
{1}",
numericUpDown1.Value, exValue.ExampleVal));
}

private void Form1_Load (object sender, EventArgs e) {
exValue = new ExampleClass ();
bindingSource1.DataSource = exValue;
}
}
}

Run the program, change the value in the numeric by typing in a value,
then close the form. You will see that the two values are different
when they should be the same.

Any thoughts on how I can correct this?
 
Reply With Quote
 
 
 
 
kremp@gmx.de
Guest
Posts: n/a
 
      9th Dec 2007
Check this out: http://support.microsoft.com/default...b;en-us;814347

Under resolution you will find the solution. Works perfect for me,
although its only a workaround :-)

I used this code (VB)

Private Sub NumericUpDown_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles
Numeric1.Validating, Numeric2.Validating, Numeric3.Validating
Dim s As String = CType(sender,
System.Windows.Forms.NumericUpDown).Value.ToString()
End Sub
 
Reply With Quote
 
Evan M.
Guest
Posts: n/a
 
      17th Dec 2007
That seems to have fixed it.

Thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BindingSource.EndEdit() throws Exception Aaron Microsoft ADO .NET 2 20th Jun 2008 02:01 PM
BindingSource.EndEdit and Position =?iso-8859-1?Q?Samuel_Garc=EDa?= Microsoft ADO .NET 0 3rd Nov 2007 08:11 PM
BindingSource determine changes before calling EndEdit() Rob Dob Microsoft Dot NET Framework Forms 2 29th Mar 2007 05:16 AM
FormClosing event doesn't work =?Utf-8?B?YXJjaHVsZXRhMzc=?= Microsoft Dot NET Framework Forms 1 8th Nov 2006 10:33 PM
endedit in formclosing Frank Microsoft C# .NET 0 28th May 2006 06:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:57 AM.