“绑定”复选框不更新其数据源 [英] Bound checkbox does not update its datasource
问题描述
设计器代码
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 =保持WebInfinity更改;
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;
保存代码
code> //注释是在保存之前加载到未选中的调用之前调用的调试器值。
private void btnApplyChanges_Click(object sender,EventArgs e)
{
(...)//其他控件的非相关保存逻辑
preProductionBindingSource.EndEdit(); // checked = false,databinding = true,datatable = true
preProductionTableAdapter.Update(salesLogix.PreProduction); // checked = false,databinding = true,datatable = true
}
发生在从未检查到检查时。我已经绑定到同一数据绑定源(我有两个组合框)的其他项目正确更新。
编辑 -
添加 cbxKeepWebInfinityChanges.DataBindings [Checked]。在$ code> preProductionBindingSource.EndEdit();
之前没有更改任何内容的WriteValue(); p>
我在使用Dathan的建议,从另一个我的问题,我试图将数据库中的一个文本是/否字段绑定到此复选框。我将其更改为正常的查询,并使用 Binding.Parse
和 Binding.Format
,并解决了我的问题。 / p>
以下是一些示例代码。
Public Form1()
{
InitializeComponent();
cbxKeepWebInfinityChanges.DataBindings [Checked]。Parse + = new ConvertEventHandler(cbxKeepWebInfinityChanges_Parse);
cbxKeepWebInfinityChanges.DataBindings [Checked]。格式+ = new ConvertEventHandler(cbxKeepWebInfinityChanges_Format);
}
void cbxKeepWebInfinityChanges_Parse(object sender,ConvertEventArgs e)
{
if((bool)e.Value == true)
e.Value =是;
else
e.Value =否;
}
void cbxKeepWebInfinityChanges_Format(object sender,ConvertEventArgs e)
{
if((string)e.Value ==Yes)
e.Value = true ;
else
e.Value = false;
}
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.
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;
}
这篇关于“绑定”复选框不更新其数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!