Question on simple binding
-
Can I simple-bind an object that derives from System.Windows.Forms.UserControls class? Thank you, Brett
Bind data to it, or bind it's properties to something else? Please be specific, as this is a rather ambiguous question. Both are possible, but rather than go into a length discussion of both, please be specific. Also take a look at the
Control.DataBindings
property in the .NET Framework SDK that gets installed by default with VS.NET.Microsoft MVP, Visual C# My Articles
-
Bind data to it, or bind it's properties to something else? Please be specific, as this is a rather ambiguous question. Both are possible, but rather than go into a length discussion of both, please be specific. Also take a look at the
Control.DataBindings
property in the .NET Framework SDK that gets installed by default with VS.NET.Microsoft MVP, Visual C# My Articles
Thanks for the response. I has more of an explaination here but didn't receive any replies. I have created a UserControl which contains a ComboBox. This combo box if filled with data in the controls constructor. I created a public property to expose the ComboBox.SelectedValue property so I may bind to it from forms which use the control. The bindings never work. The debugger doesn't compain at all, and the values in the UserControl's ComboBox never changes with the CurrencyManager.Position changes.
-
Thanks for the response. I has more of an explaination here but didn't receive any replies. I have created a UserControl which contains a ComboBox. This combo box if filled with data in the controls constructor. I created a public property to expose the ComboBox.SelectedValue property so I may bind to it from forms which use the control. The bindings never work. The debugger doesn't compain at all, and the values in the UserControl's ComboBox never changes with the CurrencyManager.Position changes.
Using the
DataBindings
property like so doesn't work?:myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");
Microsoft MVP, Visual C# My Articles
-
Using the
DataBindings
property like so doesn't work?:myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");
Microsoft MVP, Visual C# My Articles
Correct, that binding will not work, hence my dilemma. Neither of these work: myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1"); myUserControl1.DataBindings.Add( new System.Windows.Forms.Bindings("SelectedValue", dataSet1, "DataTable1")); Note, setting a value directly to the UserControl property does work. Thanks for your assistence, Brett
-
Correct, that binding will not work, hence my dilemma. Neither of these work: myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1"); myUserControl1.DataBindings.Add( new System.Windows.Forms.Bindings("SelectedValue", dataSet1, "DataTable1")); Note, setting a value directly to the UserControl property does work. Thanks for your assistence, Brett
Both of those overloads to the same thing. The first actually uses the params to construct a
Binding
instance and then calls the second overload. The thing about binding contexts is that they must be bound exactly the same. So, if you bind aComboBox
to aDataTable
, then theBinding
must also be bound to aDataTable
. I threw together a quick example. TheComboBox.DataSource
is bound to aDataTable
(in aDataSet
). TheDisplayMember
andValueMember
specify column names (say, "ID" and "Name"). I also defined a property (SelectedValue
) that reflectsComboBox.SelectedValue
and created a binding in my ctor like so:this.DataBindings.Add("SelectedValue", dataSet1, "Table1.ID");
The really important part is how you get the binding context. For the back and previous buttons, I threw together this:
private void button1_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1]; // typed dataset, so table is a prop
if (cm != null)
if (cm.Position >= 1) cm.Position--;
}private void button2_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1]; // typed dataset, so table is a prop
if (cm != null)
if (cm.Position < cm.Count - 1) cm.Position++;
}Notice that the
BindingContext
is the table itself - the same as was bound to theComboBox
.Microsoft MVP, Visual C# My Articles
-
Both of those overloads to the same thing. The first actually uses the params to construct a
Binding
instance and then calls the second overload. The thing about binding contexts is that they must be bound exactly the same. So, if you bind aComboBox
to aDataTable
, then theBinding
must also be bound to aDataTable
. I threw together a quick example. TheComboBox.DataSource
is bound to aDataTable
(in aDataSet
). TheDisplayMember
andValueMember
specify column names (say, "ID" and "Name"). I also defined a property (SelectedValue
) that reflectsComboBox.SelectedValue
and created a binding in my ctor like so:this.DataBindings.Add("SelectedValue", dataSet1, "Table1.ID");
The really important part is how you get the binding context. For the back and previous buttons, I threw together this:
private void button1_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1]; // typed dataset, so table is a prop
if (cm != null)
if (cm.Position >= 1) cm.Position--;
}private void button2_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1]; // typed dataset, so table is a prop
if (cm != null)
if (cm.Position < cm.Count - 1) cm.Position++;
}Notice that the
BindingContext
is the table itself - the same as was bound to theComboBox
.Microsoft MVP, Visual C# My Articles
Thank you. Before I dive into changing some things around, I have a question on the BindingContext. There are 3 TextBoxes and 2 UserControls which are bound to the same DataSet.Table (fyi, my dataset is typed). Would the way I get the BindingContext effect the binding between these three different controls? In other words, the binding is working fine on the the 3 TextBoxes but is not working on the 2 UserControls. If I:
string o=""; BindingManagerBase bindingManager = this.BindingContext[DataSet, "table"]; foreach(Binding b in bindingManager.Bindings) { o += b.Control.ToString() + "\n"; } MessageBox.Show(o);
The TextBox binding show, but the UserControls do not. And obviously, the Text boxes are bound and change with .Position property. Is there anything other that a set and get required on the UserControl for the binding to work? -
Thank you. Before I dive into changing some things around, I have a question on the BindingContext. There are 3 TextBoxes and 2 UserControls which are bound to the same DataSet.Table (fyi, my dataset is typed). Would the way I get the BindingContext effect the binding between these three different controls? In other words, the binding is working fine on the the 3 TextBoxes but is not working on the 2 UserControls. If I:
string o=""; BindingManagerBase bindingManager = this.BindingContext[DataSet, "table"]; foreach(Binding b in bindingManager.Bindings) { o += b.Control.ToString() + "\n"; } MessageBox.Show(o);
The TextBox binding show, but the UserControls do not. And obviously, the Text boxes are bound and change with .Position property. Is there anything other that a set and get required on the UserControl for the binding to work?Again, take a look at the
BindingContext
property documentation in the .NET Framework SDK. It's ncessary that you bind properties to the same data source as theBindingContext
. For example, many people will bind aDataTable
to aDataGrid
(assign it toDataSource
) instead of setting theDataSource
property to theDataSet
, and the table name (typed or not) as theDataMember
name. If you then use specify a context likeBindingContext[_dataSet1_._dataTable1_]
, it will not work. A good way is to useBindingContext[_dataGrid1_.DataSource, _dataGrid1_.DataMember]
. This will make sure that the binding contexts are the same and correct.Microsoft MVP, Visual C# My Articles
-
Again, take a look at the
BindingContext
property documentation in the .NET Framework SDK. It's ncessary that you bind properties to the same data source as theBindingContext
. For example, many people will bind aDataTable
to aDataGrid
(assign it toDataSource
) instead of setting theDataSource
property to theDataSet
, and the table name (typed or not) as theDataMember
name. If you then use specify a context likeBindingContext[_dataSet1_._dataTable1_]
, it will not work. A good way is to useBindingContext[_dataGrid1_.DataSource, _dataGrid1_.DataMember]
. This will make sure that the binding contexts are the same and correct.Microsoft MVP, Visual C# My Articles
I am sorry, I don't see how this answers my question. The combobox bindings are being handled in the control. It is the 'simple data binding' to the UserControl's property I can't get to work. I don't think I would pull a BindingContext from [UserControl.myProperty] would I? I don't think I would get a useful binding manager that way. Thank you very much for helping me through this.
-
I am sorry, I don't see how this answers my question. The combobox bindings are being handled in the control. It is the 'simple data binding' to the UserControl's property I can't get to work. I don't think I would pull a BindingContext from [UserControl.myProperty] would I? I don't think I would get a useful binding manager that way. Thank you very much for helping me through this.
As I said in my previous reply[^], the binding contexts have to be exactly the same object with exactly the same data member (like a table name or collection property name or something). You can't bind to a
DataTable
in aDataSet
in one case when the other binding is to theDataSet
itself. I wrote a quick example and it works fine. If you reply to this via email (use the email link) I can grab your email address and send it to you to examine the source (you won't be able to run it since the connection string and command are specific to our database here, but you can always change it).Microsoft MVP, Visual C# My Articles
-
As I said in my previous reply[^], the binding contexts have to be exactly the same object with exactly the same data member (like a table name or collection property name or something). You can't bind to a
DataTable
in aDataSet
in one case when the other binding is to theDataSet
itself. I wrote a quick example and it works fine. If you reply to this via email (use the email link) I can grab your email address and send it to you to examine the source (you won't be able to run it since the connection string and command are specific to our database here, but you can always change it).Microsoft MVP, Visual C# My Articles
I tried someting new. I created a new UserControl, but instead of inheriting from
System.Windows.Forms.UserControl
I inherited fromSystem.Windows.Forms.ComboBox
(isn't this now a customcontrol??) Anyway this new user control binds without any problems. I bind to lp2.SelectedValue Here are the basics.public class lp2 : System.Windows.Forms.ComboBox { // Our Variables private SqlDataAdapter dataAdapter; private SqlConnection sqlConnection; private DataSet dataset; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public lp2() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call sqlConnection = Utilities.dbUtilities.getWebDbCn(); string selectCommand = "select * from lang"; dataset = new DataSet(); dataAdapter = new SqlDataAdapter(selectCommand, sqlConnection); dataAdapter.Fill(dataset, "lang"); this.DataSource = dataset; this.DisplayMember = "lang.langText"; this.ValueMember = "lang.lang"; } }
My other UserControl is simular except I have a ComboBox added to the class where this class inherits ComboBox. Now I guess I need to figure out how to get the older UserControl to work since I will be back to the issue when I have a UserControl with more than one control or component on it. Are there things I must do in a UserControl to make sure I can bind to it's properties?