数据源和显示成员 [英] Data Source and Display member
问题描述
嗨!
我是C#的新手,并且出现此错误:
System.Windows.Forms.TextBox'不包含'DataSource'的定义,也找不到扩展方法'DataSource'接受类型为'System.Windows.Forms.TextBox'的第一个参数(您是否缺少using指令或程序集引用?)
有人可以帮我吗?
私有 对象发送者,EventArgs e) { 字符串 _strQuery = " ; _strQuery = " ; txtEmailAdres.DataSource = this .objBasa.Read(_strQuery).Tables [ 0 ].DefaultView; txtEmailAdresa.DisplayMember = " ; }
这是代码的一部分
TextBox
控件没有DataSource
属性或DataSource
方法,因此会引发上述错误.对于数据绑定TextBox
,我们必须使用
TextBox
的DataBindings
收集属性,如下所示
textBox1.DataBindings.Add _ (新建绑定(" , ds," ))
在这里说明
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx [ ^ ]
我认为此代码项目文章可能对您有所帮助
.NET Windows表单中的数据绑定概念 [ ^ ] 我假设您使用的是WPF,如果使用绑定,则可能使用的是MVVM,因此想在XAML中定义绑定.对于wpf,您将要做的是使用绑定:
<textbox text="{Binding textBoxSource}" />
注意:要使此方法起作用,您需要将表单的DataContext设置为包含属性textBoxSource
和textBoxSource
的类,这必须是适当的.绑定不适用于字段或方法.
Hi!
I''m new in C# and I have this error:
System.Windows.Forms.TextBox' does not contain a definition for 'DataSource' and no extension method 'DataSource' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)
Can somebody help me?
private void frmLogin_Load(object sender, EventArgs e) { string _strQuery = ""; _strQuery = "SELECT * FROM tblMembers"; txtEmailAdres.DataSource = this.objBasa.Read(_strQuery).Tables[0].DefaultView; txtEmailAdresa.DisplayMember = "email"; }
this is a part oft teh code
TheTextBox
control does not have theDataSource
property orDataSource
method, hence the above error is thrown. For data binding aTextBox
we have to use
DataBindings
collection property ofTextBox
as below
textBox1.DataBindings.Add _ (New Binding("Text", ds, "customers.custName"))
which is explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx[^]
I think this Code Project article may be helpful to you
Data binding concepts in .NET windows forms[^]
I would assume that you are using WPF, and if using binding you are probably using MVVM, so want to define the binding in the XAML. For wpf what you would do is use binding:
<textbox text="{Binding textBoxSource}" />
Note: for this to work, you need to set the DataContext for the form to the class containing the propertytextBoxSource
andtextBoxSource
must be a proeprty. Binding does not work on fields or methods.
这篇关于数据源和显示成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!