views:

72

answers:

1

I have a checkbox who's checked value is bound to a binding source which is bound to a boolean data table column. When I click my save button to push my changes in my data table to my sql server the value in the data table is never changed.

Designer code.

this.cbxKeepWebInfinityChanges = new System.Windows.Forms.CheckBox();
this.preProductionBindingSource = new System.Windows.Forms.BindingSource();
// 
// cbxKeepWebInfinityChanges
// 
this.cbxKeepWebInfinityChanges.AutoSize = true;
this.cbxKeepWebInfinityChanges.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.preProductionBindingSource, "WEBINFINTY_CHANGES", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.cbxKeepWebInfinityChanges.Location = new System.Drawing.Point(6, 98);
this.cbxKeepWebInfinityChanges.Name = "cbxKeepWebInfinityChanges";
this.cbxKeepWebInfinityChanges.Size = new System.Drawing.Size(152, 17);
this.cbxKeepWebInfinityChanges.TabIndex = 30;
this.cbxKeepWebInfinityChanges.Text = "Keep WebInfinity Changes";
this.cbxKeepWebInfinityChanges.UseVisualStyleBackColor = true;
this.cbxKeepWebInfinityChanges.CheckedChanged += new System.EventHandler(this.CauseApplyChangesActivation);
// 
// preProductionBindingSource
// 
this.preProductionBindingSource.AllowNew = false;
this.preProductionBindingSource.DataMember = "PreProduction";
this.preProductionBindingSource.DataSource = this.salesLogix;

Save Code

//the comments are the debugger values before the call in going from checked when loaded to unchecked when saved.
private void btnApplyChanges_Click(object sender, EventArgs e)
{
    (...) // non related saving logic for other controls
    preProductionBindingSource.EndEdit(); // checked = false, databinding = true, datatable = true
    preProductionTableAdapter.Update(salesLogix.PreProduction); // checked = false, databinding = true, datatable = true
}

The same things happens when going from unchecked to checked. Other items I have bound to the same data-binding source (I have two combo boxes) are updating correctly.

EDIT -- Adding cbxKeepWebInfinityChanges.DataBindings["Checked"].WriteValue(); before the preProductionBindingSource.EndEdit(); did not change anything.

A: 

I was using Dathan's suggestion from another one of my questions, I was trying to bind a text Yes/No field in the database to this check box. I changed it back to a normal query and used Binding.Parse and Binding.Format and it solved my issue.

Here is some sample code.

Public Form1()
{
    InitializeComponent();
    cbxKeepWebInfinityChanges.DataBindings["Checked"].Parse += new ConvertEventHandler(cbxKeepWebInfinityChanges_Parse);
    cbxKeepWebInfinityChanges.DataBindings["Checked"].Format += new ConvertEventHandler(cbxKeepWebInfinityChanges_Format);
}

void cbxKeepWebInfinityChanges_Parse(object sender, ConvertEventArgs e)
{
    if ((bool)e.Value == true)
        e.Value = "Yes";
    else
        e.Value = "No";
}
void cbxKeepWebInfinityChanges_Format(object sender, ConvertEventArgs e)
{
    if ((string)e.Value == "Yes")
        e.Value = true;
    else
        e.Value = false;
}
Scott Chamberlain