如何将数据绑定到变量 [英] How to databind to a variable

查看:84
本文介绍了如何将数据绑定到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

盖伊的

我是C#的初学者,遇到了问题.

它与数据绑定有关.我有一个程序可以从SQL Server数据库中获取数据,然后将数据放入文本框,使用以下代码,这些文本框可以正常工作.

Hi Guy’s

I am a beginner in C# and I have a problem.

It has to do with data binding. I have a program that gets data from a SQL Server database and put the data into text boxes which works OK for the text boxes using the following code.

userNameTextBox.DataBindings.Add("text",myBindingSource,"UserName");


但是我的问题是我有一个不属于文本框的数据库字段,它属于一个变量.
现在,我知道您无法将数据绑定到变量.我想做的是通过使用"myBindingSource"将数据以某种方式获取到变量中.像这样的形式.


But my problem is that I have a database field that doesn’t belong in a text box it belongs in a variable.
Now I know you cannot databind to a variable. What I want to do is get the data into a variable somehow by using the "myBindingSource". In a form something like this.

?.DataBindings.Add("text",myBindingSource,"Color");


在哪里 ?将是变量.

这是我过去所做的.


Where ? would be the variable.

Here is what I done it in the past.

colorTextBox.Text.DataBindings.Add("text",myBindingSource,"Color");  
Variable =  colorTextBox.Text;


我想找到一种不使用隐藏文本框的方法.


I would like to find a way of doing it without using hidden textboxes.
Please show me how to do it in code.

推荐答案

重要提示:请参阅下面的"...编辑1 ...".

...注意:请签入您的帖子:您的意思是在示例代码中使用文本"而不​​是文本"吗?

...评论:如果有人会发布一种更简单的方法,那将是一件令人愉快的事情! ...

是的,您可以这样做.您将需要:

1.确保您的Form或Class继承了INotifyPropertyChanged
Important: please see "... edit 1 ..." below.

... note: please check in your post: did you mean to use ''text'' instead of ''Text'' in your sample code ?

... comment: delightful if someone will post a simpler way of doing this ! ...

Yes, you can do this. You are going to need to :

1. make sure your Form or Class inherits INotifyPropertyChanged
public partial class Form1 : Form, INotifyPropertyChanged

2.为INotifyPropertyChanged实现EventHandler

2. implement an EventHandler for INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String pName)
{
  if (PropertyChanged != null)
  {
   PropertyChanged(this, new PropertyChangedEventArgs(pName));
  }
}

3.在属性设置者"中:调用实现INotifyPropertyChanged

3. in the ''setter of your property: invoke the EventHandler that implements INotifyPropertyChanged

set
{
    if (value != this.strColor)
    {
        this.strColor = value;
        NotifyPropertyChanged("ptyColor");
    }
}

4.那么您的绑定调用(也许在表单"Load"事件中?)可能看起来像:

4. then your binding call (perhaps in the Form ''Load'' event ?) might look like:

// note here we are adding to the DataBindings of the Form itself
// and the binding context is the Form which inherits INotifyPropertyChanged
// which is why the second parameter is ''this''
//
// please see comments in ... edit 1 ...
//
this.DataBindings.Add("ptyColor", this, "strColor");
//

签出:[
在那种情况下,绑定调用如下所示:

Check out:[^],[^]

... edit #1 ...

I looked up the code where I''d done something similar: my usage was to add a binding to a TextBox so that any change in a public property ''TheBackColor'' automatically set the BackColor of the TextBox.

In that case the binding call looked like this:

textBox1.DataBindings.Add("BackColor", this, "TheBackColor");

所以我现在担心我根据您的示例在此处显示的绑定调用可能需要为

So I am now have concerned that the binding call I have shown here based on your example, may need to be

this.DataBindings.Add("ptyColor", this, "ptyColor")

,如果这两种类型的调用都无济于事您实现了所追求的目标:我随时准备接受外界的黑暗投票(我拒绝蒙上眼睛,谢谢).

...结束编辑#1 ...

注意:此特定代码尚未经过测试,但是基于我希望能够清楚地记住几个月前实现此代码的记忆.注意警告.

If both those types of calls don''t help you achieve what you are after: I stand ready to be down-voted into outer-darkness (I decline the blindfold, thank you).

... end edit #1 ...

Note: this specific code has not been tested, but is based on what I hope is a clear memory of implementing this a few months ago. Caveat emptor.


如果您不想使用隐藏的文本框,则可以尝试使用动态文本框控件.

昏暗的txDirector作为新的TextBox()

txt_dinamic.DataBindings.Add(New Binding("Text",bsBus,"Fieldname",True))
Me.Controls.Add(txt_dinamic)
If you does not want to use hidden textboxes, you can try to use dynamical textbox control.

Dim txDirector As New TextBox()

txt_dinamic.DataBindings.Add(New Binding("Text", bsBus, "Fieldname", True))
Me.Controls.Add(txt_dinamic)


这篇关于如何将数据绑定到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆