[英]Bind TextBox.Text to DataSet.DataSetName
I am trying to bind a TextBox
's Text
property to a DataSet
's DataSetName
property. 我试图将
TextBox
的Text
属性绑定到DataSet
的DataSetName
属性。
I get 我懂了
System.ArgumentException: 'Cannot bind to the property or column DataSetName on the DataSource.
System.ArgumentException:'无法绑定到数据源上的属性或列DataSetName。 Parameter name: dataMember'
参数名称:dataMember'
If there a way to bind a single text box in this way? 是否可以通过这种方式绑定单个文本框? I think it has something to do with the fact that
DataSet
is a collection, so the BindingSource
is expecting to have a table bound with it, not a text box. 我认为这与
DataSet
是集合的事实有关,因此BindingSource
期望具有与其绑定的表,而不是文本框。
Can I achieve this without creating a "container" class to hold my DataSetName
property and a DataSet
? 我可以在不创建“容器”类来保存我的
DataSetName
属性和DataSet
情况下实现此目的吗?
Edit 编辑
It was silly of me to not include any code. 不包含任何代码对我来说是愚蠢的。 So here you go:
因此,您在这里:
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
is correct (not null, points top the right DataSet
) node.TableGroup
是正确的(不为null,指向右上方的DataSet
) Once the TextBox
is actually painted, I get the above exception. 一旦真正绘制了
TextBox
,我就会得到上述异常。
I am using Windows Forms with the designer, so the two first lines of code are automatically generated. 我在设计器中使用Windows窗体,因此自动生成了前两行代码。
The CurrencyManager
uses ListBindingHelper.GetListItemProperties(yourDataset)
to get its properties and since it doesn't return any property because of its type descriptor, then the data-binding fails. CurrencyManager
使用ListBindingHelper.GetListItemProperties(yourDataset)
来获取其属性,并且由于其类型描述符而没有返回任何属性,因此数据绑定失败。
You can expose DataSet
properties in a different way by using a wrapper around data set, implementing a custom type descriptor to provide data set properties: 您可以通过使用围绕数据集的包装器,实现自定义类型描述符以提供数据集属性的方式,以不同的方式公开
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;
}
}
Then use it this way: 然后以这种方式使用它:
var myDataSet = new DataSet("myDataSet");
var wrapper = new CustomObjectWrapper(myDataSet);
textBox1.DataBindings.Add("Text", wrapper, "DataSetName", true,
DataSourceUpdateMode.OnPropertyChanged);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.