This documentation is archived and is not being maintained.

DataBindingCollection.Add Method

Adds the specified DataBinding object to the DataBindingCollection.

[Visual Basic]
Public Sub Add( _
   ByVal binding As DataBinding _
)
[C#]
public void Add(
 DataBinding binding
);
[C++]
public: void Add(
 DataBinding* binding
);
[JScript]
public function Add(
   binding : DataBinding
);

Parameters

binding
The data binding object to add.

Example

[Visual Basic, C#, C++] The following example creates a new DataBinding object, binding1, by setting it equal to the object obtained using the SyncRoot method. It then uses the Add method to add binding1 and another DataBinding object, named binding, to the DataBindingCollection for a Control object.

[Visual Basic] 
' Create a Text property with accessors that obtain 
' the property value from and set the property value
' to the Text key in the DataBindingCollection class.

Public Property [Text]() As String
   Get
      Dim myBinding As DataBinding = DataBindings("Text")
      If Not (myBinding Is Nothing) Then
         Return myBinding.Expression
      End If
      Return String.Empty
   End Get
   Set
      
      If value Is Nothing OrElse value.Length = 0 Then
         DataBindings.Remove("Text")
      Else
         
         Dim binding As DataBinding = DataBindings("Text")
         
         If binding Is Nothing Then
            binding = New DataBinding("Text", GetType(String), value)
         Else
            binding.Expression = value
         End If
         ' Call the DataBinding constructor, then add
         ' the initialized DataBinding object to the 
         ' DataBindingCollection for this custom designer.
         Dim binding1 As DataBinding = CType(DataBindings.SyncRoot, DataBinding)
         DataBindings.Add(binding)
         DataBindings.Add(binding1)
      End If
      OnBindingsCollectionChanged("Text")
   End Set
End Property


[C#] 
// Create a Text property with accessors that obtain 
// the property value from and set the property value
// to the Text key in the DataBindingCollection class.
public string Text
 {
    get
    {
       DataBinding myBinding = DataBindings["Text"];
       if (myBinding != null)
       {
          return myBinding.Expression;
       }
       return String.Empty;
    }
    set
    {

       if ((value == null) || (value.Length == 0))
       {
          DataBindings.Remove("Text");
       }
       else
       {

          DataBinding binding = DataBindings["Text"];

          if (binding == null)
          {
             binding = new DataBinding("Text", typeof(string),value );
          }
          else
          {
             binding.Expression = value;
          }
          // Call the DataBinding constructor, then add
          // the initialized DataBinding object to the 
          // DataBindingCollection for this custom designer.
          DataBinding binding1=(DataBinding)DataBindings.SyncRoot;
          DataBindings.Add(binding);
          DataBindings.Add(binding1);
       }
       OnBindingsCollectionChanged("Text");
    }
 }

[C++] 
// Create a Text property with accessors that obtain
// the property value from and set the property value
// to the Text key in the DataBindingCollection class.
 public:
__property String* get_Text() {
   DataBinding* myBinding = DataBindings->Item[S"Text"];
   if (myBinding != 0) {
      return myBinding->Expression;
   }
   return String::Empty;
}
__property void set_Text(String* value) {

   if ((value == 0) || (value->Length == 0)) {
      DataBindings->Remove(S"Text");
   } else {

      DataBinding* binding = DataBindings->Item[S"Text"];

      if (binding == 0) {
         binding = new DataBinding(S"Text", __typeof(String), value);
      } else {
         binding->Expression = value;
      }
      // Call the DataBinding constructor, then add
      // the initialized DataBinding object to the
      // DataBindingCollection for this custom designer.
      DataBinding* binding1=dynamic_cast<DataBinding*>(DataBindings->SyncRoot);
      DataBindings->Add(binding);
      DataBindings->Add(binding1);
   }
   OnBindingsCollectionChanged(S"Text");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

DataBindingCollection Class | DataBindingCollection Members | System.Web.UI Namespace

Show: