gpt4 book ai didi

c# - 将 TextBox.Text 绑定(bind)到 DataSet.DataSetName

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

我正在尝试将 TextBoxText 属性绑定(bind)到 DataSetDataSetName 属性。

我明白了

System.ArgumentException: 'Cannot bind to the property or column DataSetName on the DataSource. Parameter name: dataMember'

有没有办法这样绑定(bind)单个文本框?我认为这与 DataSet 是一个集合这一事实有关,因此 BindingSource 期望绑定(bind)一个表,而不是文本框。

我能否在不创建“容器”类来保存我的 DataSetName 属性和 DataSet 的情况下实现这一目标?

编辑

不包含任何代码对我来说很愚蠢。所以给你:

this.tableGroupBindingSource.DataSource = typeof(DataSet);
...
this.TableGroupNameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.tableGroupBindingSource, "DataSetName", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
...
tableGroupBindingSource.DataSource = node.TableGroup;
  • node.TableGroup 正确(不为空,指向右上DataSet)

一旦实际绘制了 TextBox,就会出现上述异常。

我在设计器中使用 Windows 窗体,因此前两行代码是自动生成的。

最佳答案

CurrencyManager 使用 ListBindingHelper.GetListItemProperties(yourDataset) 来获取它的属性,由于它的类型描述符不返回任何属性,因此数据绑定(bind)失败。

您可以通过使用数据集包装器以不同的方式公开 DataSet 属性,实现自定义类型描述符以提供数据集属性:

using System;
using System.ComponentModel;
public class CustomObjectWrapper : CustomTypeDescriptor
{
public object WrappedObject { get; private set; }
public CustomObjectWrapper(object o) : base()
{
WrappedObject = o ?? throw new ArgumentNullException(nameof(o));
}
public override PropertyDescriptorCollection GetProperties()
{
return this.GetProperties(new Attribute[] { });
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(WrappedObject, true);
}
public override object GetPropertyOwner(PropertyDescriptor pd)
{
return WrappedObject;
}
}

然后这样使用:

var myDataSet = new DataSet("myDataSet");
var wrapper = new CustomObjectWrapper(myDataSet);
textBox1.DataBindings.Add("Text", wrapper, "DataSetName", true,
DataSourceUpdateMode.OnPropertyChanged);

关于c# - 将 TextBox.Text 绑定(bind)到 DataSet.DataSetName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51865693/

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