【发布时间】:2014-04-30 02:02:30
【问题描述】:
我想自定义将新项目添加到 BindingSource(所有强类型)中,如以下 MSDN 文章所述:
How to: Customize Item Addition with the Windows Forms BindingSource
下面的代码导致InvalidOperationException:添加到 BindingSource 列表的对象必须全部属于同一类型。此外,对象myTypesBindingSource.Current 似乎是DataRowView,里面有我的相关行。
如何自定义添加强类型BindingSource?
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.someDataSet = new myDB.SomeDataSet();
this.myTypesBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.myTypesTableAdapter = new myDB.SomeDataSetTableAdapters.myTypesTableAdapter();
this.tableAdapterManager = new myDB.SomeDataSetTableAdapters.TableAdapterManager();
this.myTypesBindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);
this.someIntValueTextBox = new System.Windows.Forms.TextBox();
// someDataSet
this.someDataSet.DataSetName = "SomeDataSet";
this.someDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
// myTypesBindingSource
// As generated:
// this.myTypesBindingSource.DataMember = "myTypes";
// this.myTypesBindingSource.DataSource = this.someDataSet;
this.myTypesBindingSource.DataSource = this.someDataSet;
this.myTypesBindingSource.AddingNew += new System.ComponentModel.AddingNewEventHandler(this.myTypesBindingSource_AddingNew);
// myTypesTableAdapter
this.myTypesTableAdapter.ClearBeforeFill = true;
// tableAdapterManager
this.tableAdapterManager.BackupDataSetBeforeUpdate = false;
this.tableAdapterManager.myTypesTableAdapter = this.myTypesTableAdapter;
this.tableAdapterManager.UpdateOrder = myDB.SomeDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete;
// someIntValueTextBox
this.someIntValueTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myTypesBindingSource, "someIntValue", true));
this.someIntValueTextBox.Name = "someIntValueTextBox";
}
private void myTypesBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
SomeDataSet.myTypesRow newRow = someDataSet.myTypes.NewmyTypesRow();
newRow.someIntValue = 99;
e.NewObject = newRow;
}
【问题讨论】:
-
+1 这让我进一步了解了我从未经历过的有关 DataSet 和 DataBinding 的详细信息。谢谢你也让我学习。
标签: c# winforms data-binding .net-4.5 bindingsource