This documentation is archived and is not being maintained.

BindingContext Constructor

Initializes a new instance of the BindingContext class.

[Visual Basic]
Public Sub New()
[C#]
public BindingContext();
[C++]
public: BindingContext();
[JScript]
public function BindingContext();

Remarks

Create a new BindingContext and set it to the BindingContext property of an object that inherits from Control when you want to have multiple BindingManagerBase instances for the same data source.

For example, if you have two BindingManagerBase objects (from two different BindingContext objects), you can set the Position properties of each BindingManagerBase to different values causing each set of data-bound controls will display different values from the same data source.

Example

The following example creates two new BindingContext objects, and assigns each object to the BindingContext property of a GroupBox control. GroupBox1 contains TextBox1, and GroupBox2 contains TextBox2 (which is accomplished by using the AddRange method of the Control.ControlCollection). The example then adds Binding objects to the two TextBox controls, binding each to the same data source and data member. The example also shows two event handlers that use the BindingContext from the GroupBox controls to set the Position property on different BindingManagerBase objects.

[Visual Basic] 
Private Sub BindControls()
    Dim bcG1 As New BindingContext()
    Dim bcG2 As New BindingContext()
       
    groupBox1.BindingContext = bcG1
    groupBox2.BindingContext = bcG2
       
    textBox1.DataBindings.Add("Text", ds, "Customers.CustName")
    textBox2.DataBindings.Add("Text", ds, "Customers.CustName")
End Sub    
   
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    groupBox1.BindingContext(ds, "Customers").Position += 1
End Sub    
   
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    groupBox2.BindingContext(ds, "Customers").Position += 1
End Sub


[C#] 
private void BindControls()
{
   BindingContext bcG1 = new BindingContext();
   BindingContext bcG2 = new BindingContext();

   groupBox1.BindingContext = bcG1;
   groupBox2.BindingContext = bcG2;

   textBox1.DataBindings.Add("Text", ds, "Customers.CustName");
   textBox2.DataBindings.Add("Text", ds, "Customers.CustName");
}

private void Button1_Click(object sender, EventArgs e)
{
   groupBox1.BindingContext[ds, "Customers"].Position += 1;         
}

private void Button2_Click(object sender, EventArgs e)
{
   groupBox2.BindingContext[ds, "Customers"].Position += 1;
}

[C++] 
private:
void BindControls()
{
   System::Windows::Forms::BindingContext* bcG1 = new System::Windows::Forms::BindingContext();
   System::Windows::Forms::BindingContext* bcG2 = new System::Windows::Forms::BindingContext();

   groupBox1->BindingContext = bcG1;
   groupBox2->BindingContext = bcG2;

   textBox1->DataBindings->Add(S"Text", ds, S"Customers.CustName");
   textBox2->DataBindings->Add(S"Text", ds, S"Customers.CustName");
}

void Button1_Click(Object* /*sender*/, EventArgs* /*e*/)
{
   groupBox1->BindingContext->get_Item(ds, S"Customers")->Position = 
      groupBox1->BindingContext->get_Item(ds, S"Customers")->Position + 1;
}

void Button2_Click(Object* /*sender*/, EventArgs* /*e*/)
{
   groupBox2->BindingContext->get_Item(ds, S"Customers")->Position =
      groupBox2->BindingContext->get_Item(ds, S"Customers")->Position + 1;
}

[JScript] 
private function BindControls()
{
   var bcG1 : System.Windows.Forms.BindingContext = new System.Windows.Forms.BindingContext;
   var bcG2 : System.Windows.Forms.BindingContext = new System.Windows.Forms.BindingContext;

   button1.BindingContext = bcG1;
   button2.BindingContext = bcG2;

   textBox1.DataBindings.Add(new Binding
      ("Text", ds, "customers.custName"));
   textBox2.DataBindings.Add(new Binding
      ("Text", ds, "customers.custName"));

   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this.BindingContext [ds, "Customers"];
}

private function Button1_Click(sender, e : EventArgs)
{
   bmCustomers.Position += 1;  
}

private function Button2_Click(sender, e : EventArgs)
{
   bmCustomers.Position -= 1;
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

BindingContext Class | BindingContext Members | System.Windows.Forms Namespace | BindingManagerBase

Show: