我想自定義添加新項目的成BindingSource
爲以下MSDN文章描述(所有強類型):在InvalidOperationException
自定義StronglyTyped的BindingSource添加項目
How to: Customize Item Addition with the Windows Forms BindingSource
以下結果的代碼:添加到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本作我調查了關於DataSets和DataBinding的進一步細節,這是我從未經歷過的。謝謝你讓我學習。 –