首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定的复选框不会更新其数据源

绑定的复选框不会更新其数据源
EN

Stack Overflow用户
提问于 2010-03-26 02:06:49
回答 1查看 2.7K关注 0票数 1

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

设计器代码。

代码语言:javascript
复制
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;

保存代码

代码语言:javascript
复制
//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();不会更改任何内容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-26 03:18:46

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

下面是一些示例代码。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2518191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档