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
David daavenaPosted Nov 27, 2007, 4:10 AM
It's working.
THANK YOU FOR HELP.
David
AlanPosted Nov 26, 2007, 10:53 AM
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 daavenaPosted Nov 26, 2007, 1:49 AM
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.
AlanPosted Nov 25, 2007, 11:01 AM
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?