C# Databinding Issue

J

Jeff

I am having a behaviour problem with Databinding on a C# WinForm. I
have three textboxes and a Checkbox bound to a dataset. The desired
behaviour is when I check/uncheck the checkbox, I want to either
enable, or disable and clear the corresponding textboxes.

I want the fact that I cleared the text in the textboxes to reflect in
the dataset, so I am calling the EndCurrentEdit() method on the
currency manager to update the dataset within the event on the
checkbox. The problem is the checkbox will not check/uncheck properly
due to the fact the currency manager is firing. If I click on the
checkbox to uncheck it in runtime, the textboxes clear, but the
checkbox remains in a checked state.

Am I doing this properly by calling EndCurrentEdit() on the currency
manager? How do I reflect the clearing of the textboxes in the
dataset? I should clarify, that if I clear the textboxes in code, when
the focus of the form changes to a bound control, it replaces the text
back into the cleared textboxes.


//method called when the form is loading to create the bindings
private void BindControls()
{
textbox1.DataBindings.Add(new Binding("Text", _myDataSet,
"MyTable.Field1"));
textbox2.DataBindings.Add(new Binding("Text", _myDataSet,
"MyTable.Field2"));
textbox3.DataBindings.Add(new Binding("Text", _myDataSet,
"MyTable.Field3"));
checkbox1.DataBindings.Add(new Binding("Checked", _myDataSet,
"MyTable.Field4"));

//currency manager declared a member variable tothe form
_mCurrencyManager = null;
_mCurrencyManager = (CurrencyManager)this.BindingContext[_myDataSet,
"MyTable"];
}

//event on the check box which fires when checked/unchecked
private void chkCustomAssembly_CheckedChanged(object sender,
System.EventArgs e)
{
if(chkCustomAssembly.Checked == true)
{
textbox1.Enabled = true;
textbox2.Enabled = true;
textbox3.Enabled = true;
}
else
{
textbox1.Text = "";
textbox2.Text = "";
textbox3.Text = "";

textbox1.Enabled = false;
textbox2.Enabled = false;
textbox3.Enabled = false;
}

//update the binding currency manager to reflect the changes
//this call causes a problem with the behaviour of the checkbox
_mCurrencyManager.EndCurrentEdit();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top