Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Data binding and lost focus
Message
De
05/07/2007 16:49:39
 
 
À
05/07/2007 07:18:28
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01237872
Message ID:
01238091
Vues:
7
The problem in this case is that pressing a ToolBar button does not cause the ActiveControl to lose focus (same with menu items), thus not forcing the control's value into it's databound object. You need something that will force this for every control you have. It can get complicated.

I have a method that I have named ForceBind() in most of my controls (those that have it implement an Interface I have created for this type of behavior). If the control is a container object that implements that Interface (such as a Panel, UserControl, etc.) it will call the ForceBind() method of *it's* ActiveControl (if *that* control implements the Interface).

The actual ForceBind() method is pretty complicated in my controls and relies on the fact that I have a DataBind() method on my controls that tell it which DataTable and DataColumn I am binding that control to. However, I've done a little experimenting and I see that we can find other ways of finding those fields. Here's the ForceBind() method for MyTextBox (along with the code for determining the m_BoundTable, m_BoundColumn and m_BoundProperty fields):
private	DataTable m_BoundTable    = (DataTable)this.DataBindings[0].DataSource;
private	string    m_BoundColumn   = this.DataBindings[0].DataBindings[0].BindingMemberInfo.BindingMember;
private	string    m_BoundProperty = this.DataBindings[0].PropertyName;
private	CurrencyManager oCurrency = (CurrencyManager)this.BindingContext[m_BoundTable];


public virtual void ForceBind()
{
	if (this.DataBindings.Count == 0)
		return;


	int nRow = -1;
	if (this.m_BoundTable != null && this.m_BoundColumn != "")
	{
		if (this.oCurrency != null)
			nRow = this.oCurrency.Position;

		if (nRow >= 0)
		{
			DataRow row = this.m_BoundTable.DefaultView[nRow].Row;
			object oValue = row[this.m_BoundColumn];
			switch (this.m_BoundProperty)
			{
				case "Text" :
					oValue = this.Text;
					break;
				case "Tag" :
					oValue = this.Tag;
					break;
			}

			if (oValue == null)
				oValue = DBNull.Value;

			System.RuntimeTypeHandle handle = System.Type.GetTypeHandle(this);
			System.Type eType = System.Type.GetTypeFromHandle(handle);

			ConvertEventArgs e = new ConvertEventArgs(oValue, eType);

			this.ParseHandler(this, e);
			if (row[this.m_BoundColumn] != e.Value)
			{
				try
				{
					row[this.m_BoundColumn] = e.Value;
				}
				catch (Exception)
				{ }
						
				this.OnValidated(new EventArgs());
			}
		}
	}
}
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform