【问题标题】:Bound checkbox does not update its datasource绑定复选框不更新其数据源
【发布时间】:2011-01-31 20:53:02
【问题描述】:

我有一个复选框,它的选中值绑定到绑定到布尔数据表列的绑定源。当我单击保存按钮将数据表中的更改推送到我的 sql 服务器时,数据表中的值永远不会更改。

设计器代码。

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;

保存代码

//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
}

从未选中变为选中时会发生同样的事情。我绑定到同一数据绑定源的其他项目(我有两个组合框)正在正确更新。

编辑—— 在preProductionBindingSource.EndEdit(); 之前添加cbxKeepWebInfinityChanges.DataBindings["Checked"].WriteValue(); 并没有改变任何东西。

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    我正在使用来自another one of my questions 的 Dathan 的建议,我试图将数据库中的文本 Yes/No 字段绑定到此复选框。我将其改回普通查询并使用Binding.ParseBinding.Format,它解决了我的问题。

    这是一些示例代码。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 2013-08-18
      • 2014-02-17
      • 2011-02-02
      • 2017-07-15
      • 2011-03-05
      • 2011-08-28
      相关资源
      最近更新 更多