嗨! 我是C#的新手,并且出现此错误:
System.Windows.Forms.TextBox'不包含'DataSource'的定义,也找不到扩展方法'DataSource'接受类型为'System.Windows.Forms.TextBox'的第一个参数(您是否缺少using指令或程序集引用?)有人可以帮我吗?
解决方案私有 无效 frmLogin_Load(对象发送者,EventArgs e) { 字符串 _strQuery = " " ; _strQuery = " SELECT * FROM tblMembers" ; txtEmailAdres.DataSource = this .objBasa.Read(_strQuery).Tables [ 0 ].DefaultView; txtEmailAdresa.DisplayMember = " 电子邮件" ; }
这是代码的一部分
TextBox 控件没有DataSource 属性或DataSource 方法,因此会引发上述错误.对于数据绑定TextBox ,我们必须使用 TextBox的DataBindings 收集属性,如下所示 textBox1.DataBindings.Add _ (新建绑定(" 文本" , ds," customers.custName" ))
在这里说明 msdn.microsoft/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
The TextBox control does not have the DataSource property or DataSource method, hence the above error is thrown. For data binding a TextBox we have to use DataBindings collection property of TextBox as below textBox1.DataBindings.Add _ (New Binding("Text", ds, "customers.custName"))which is explained here msdn.microsoft/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 property textBoxSource and textBoxSource must be a proeprty. Binding does not work on fields or methods.
更多推荐
数据源和显示成员
发布评论