Andrus,
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 found a solution for this for the DataGrid, but you need something 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, remove the the "this" from those variables if you use this code):
public virtual void ForceBind()
{
if (this.DataBindings.Count == 0)
return;
DataTable m_BoundTable = (DataTable)this.DataBindings[0].DataSource;
string m_BoundColumn = this.DataBindings[0].DataBindings[0].BindingMemberInfo.BindingMember;
string m_BoundProperty = this.DataBindings[0].PropertyName;
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