数据源和显示成员

编程入门 行业动态 更新时间:2024-10-26 20:28:42
本文介绍了数据源和显示成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 我是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表单中的数据绑定概念 [ ^ ] <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.

更多推荐

数据源和显示成员

本文发布于:2023-08-05 21:39:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!