Hi,
I have a small problem with databindings.I would like to bind texbox.Text and myClass.Somevariable.
I tried textbox.databindings("Text", myClass,"Somevariable"). This doesn't work.
I also tried google it. It gave me some articles how to bind datagrid or some thing like that.
Can you help me?
Thanks.
David.
Loading

AlanRank #669 · 3.6K reputationPosted Nov 25, 2007, 6:01 AM EST
The most likely reason for this problem, David, is that someVariable is a field rather than a property. It has to be a property of an object for databinding to work.
Also I take it you've used an object variable rather than the class name for the second parameter?
David daavena5 reputationPosted Nov 25, 2007, 8:49 PM EST
thanks for your response
The second parameter of textbox.databindings.add() is an object.
I made someVarialble as a property.
...
privata string someVar="abc";
public string someVariable{get{return someVar;}}
....
somewhere in the code/class this.someVar+="xyz";
...
in the form(load method)
textbox.databindings.Add("Text",myOBJ,"someVariable");
I still see only abc.
Thank you.
David.
AlanRank #669 · 3.6K reputationPosted Nov 26, 2007, 5:53 AM EST
I suspect you're missing something else here, David, which is a way for the myClass object to notify bound controls that its someVariable property has changed.
If you're using .NET 2.0 or later this can be done by MyClass implementing the System.ComponentModel.INotifyPropertyChanged interface. All you need to do is to change the code for MyClass as follows (everything else is taken care of behind the scenes):
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
private string someVar = "abc";
public string someVariable
{
get { return someVar; }
set
{
someVar = value;
NotifyPropertyChanged("someVariable");
}
}
// other code
}
David daavena5 reputationPosted Nov 26, 2007, 11:10 PM EST
It's working.
THANK YOU FOR HELP.
David