Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
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.
C#
userNameTextBox.DataBindings.Add("text",myBindingSource,"UserName");

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.
C#
?.DataBindings.Add("text",myBindingSource,"Color");

Where ? would be the variable.

Here is what I done it in the past.
C#
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.
Posted
Updated 21-Dec-11 20:46pm
v6
Comments
Rajesh Anuhya 22-Dec-11 2:49am    
What happened to this Question??

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. implement an EventHandler for INotifyPropertyChanged
C#
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String pName)
{
  if (PropertyChanged != null)
  {
   PropertyChanged(this, new PropertyChangedEventArgs(pName));
  }
}
3. in the 'setter of your property: invoke the EventHandler that implements INotifyPropertyChanged
C#
set
{
    if (value != this.strColor)
    {
        this.strColor = value;
        NotifyPropertyChanged("ptyColor");
    }
}
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")
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.
 
Share this answer
 
v3
Comments
thatraja 13-Nov-11 0:01am    
Nice effort, 5!
Monjurul Habib 13-Nov-11 3:28am    
my 5+
gary4 13-Nov-11 14:57pm    
Thanks Bill for your answer. I will try and understand your code.
I would think Microsoft would come up with an easier solution.
This is what I did in the past. Both had disadvantages and I always wanted to bind directly to the variable.
Here is what I done it in the past.

VarTextBox.Text.DataBindings.Add("text",myBindingSource,"UserName");
Variable = VarTextBox.Text;

It has the advantages of only adding one more line of code.
But the disadvantages are you got a lot of hidden TextBox on your Form.

The second way is.

myVar = dataset1.Tables[0].Rows[1]["columnname"].ToString();

The disadvantages that I see are that you have to keep track of the table, rows and column yourself.

I sure you are a where of both of these methods.
I thought I would share these methods for anyone that that was looking for a shorter but maybe not the best way of do it.
Thanks again
gary4 13-Nov-11 17:33pm    
Bill
I don’t understand your code, I try it and I got no results. How do you get data from the Db???
Maybe I wasn’t clear enough in my question but I also want to get data from the Db.
BillWoodruff 14-Nov-11 7:47am    
Sorry Gary, my answer assumed you had access to the piece of data of interest, and all you wanted to do was to make it persist in a way that did not require a TextBox.

I do not feel qualified to go beyond the scope of my current answer. Accessing the DB goes into ? possibly many issues dependent on the DB and its structure, and the nature of your binding/access-layer to it ?

Hope you do find an answer that will work for you.

best, Bill
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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900