gpt4 book ai didi

c# - 自定义 StronglyTyped BindingSource 项添加

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:03 25 4
gpt4 key购买 nike

我想自定义将新项目添加到 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;
}

最佳答案

在示例中,它不是强类型 BindingSource .事实上,AddingNewEventArgs.NewObject属性是一个对象。因此,为它分配任何派生类型都可以。

另外,请注意该示例使用类对象 DemoCustomer,它不是返回 DataRowDataSet.Tables[0].Row .在我看来,当使用 DataSet 时,游戏有点不同。 .

当使用DataSet 时,您只需设置一个DataTable。作为 BindingSource.DataSource ,让你写这样的东西:

BindingSource.DataSource = DataSet.Tables[0];

这样,当您将项目添加到 BindingSource.List 时使用 BindingSource.AddNew()BindingSource“知道”它有一个DataTable作为它的DataSource,所以它调用DataTable.NewRow()方法和一个新的 DataRow 添加到您的 DataTable!因此,需要处理 DataRow 而不是简单的对象。

Working with a DataRow

如果您想执行与 MSDN 上的示例类似的操作,则必须自己创建该行。

DataRow newRow = DataSet.Tables[0].NewRow();
newRow.Columns["intColumn"] = 99;
e.NewObject = newRow;

这样,您就可以知道您想要什么默认值。

否则,如果不行,你不妨试试这个:

var newRow = (DataRow)e.NewObject;
newRow["intColumn"] = 99;

这里的弱点是,每当您更改基础数据库表列名称时,您都必须来到这里,更改您的 intColumn 的名称,然后重新编译和重新部署。

此外,这种情况不会经常发生,因此根据您的环境情况,这可能是值得的。

EDIT #1

在更加关注之后:

Also, the object myTypesBindingSource.Current seems to be a DataRowView with my relevant row inside

来自 MSDN: DataRowView

Whenever data is displayed, such as in a DataGrid control, only one version of each row can be displayed. The displayed row is a DataRowView.

A DataRowView can have one of four different version states: Default, Original, Current, and Proposed.

After invoking BeginEdit on a DataRow, any edited value becomes the Proposed value. Until either CancelEdit or EndEdit is invoked, the row has an Original and a Proposed version. If CancelEdit is invoked, the proposed version is discarded, and the value reverts to Original. If EndEdit is invoked, the DataRowView no longer has a Proposed version; instead, the proposed value becomes the current value. Default values are available only on rows that have columns with default values defined.

这意味着当添加新行时,您实际上是在添加一个 DataRowView。您可以通过访问其 DataRowView.Row 来访问当前行属性(property)。

考虑到这一点,您可能会更改我最初对此的回答中提出的解决方案:

var newRow = ((DataRowView)e.NewObject).Row;
newRow.Columns["intColumn"] = 99;

编辑:(史蒂文)最终代码

DataView dv = (DataView)myTypesBindingSource.List;
DataRowView drv = dv.AddNew();
SomeDataSet.myTypesRow newMyTypesRow = (SomeDataSet.myTypesRow)drv.Row;
newMyTypesRow.someIntValue = 53;
e.NewObject = drv;
myTypesBindingSource.MoveLast();

关于c# - 自定义 StronglyTyped BindingSource 项添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com