BindingSource.Add(Object) Method

Definition

Adds an existing item to the internal list.

C#
public virtual int Add(object value);
C#
public virtual int Add(object? value);

Parameters

value
Object

An Object to be added to the internal list.

Returns

The zero-based index at which value was added to the underlying list represented by the List property.

Implements

Exceptions

value differs in type from the existing items in the underlying list.

Examples

The following code example how to use the Add method. To run this example, paste the code into a Windows Form, and call the PopulateBindingSourceWithFonts method from the form's constructor.

C#
  public BindingSource bindingSource1 = new BindingSource();
  TextBox box1 = new TextBox();

  private void PopulateBindingSourceWithFonts()
  {
      bindingSource1.CurrentChanged += new EventHandler(bindingSource1_CurrentChanged);
      bindingSource1.Add(new Font(FontFamily.Families[2], 8.0F));
      bindingSource1.Add(new Font(FontFamily.Families[4], 9.0F));
      bindingSource1.Add(new Font(FontFamily.Families[6], 10.0F));
      bindingSource1.Add(new Font(FontFamily.Families[8], 11.0F));
      bindingSource1.Add(new Font(FontFamily.Families[10], 12.0F));
      DataGridView view1 = new DataGridView();
      view1.DataSource = bindingSource1;
      view1.AutoGenerateColumns = true;
      view1.Dock = DockStyle.Top;
      this.Controls.Add(view1);
      box1.Dock = DockStyle.Bottom;
      box1.Text = "Sample Text";
      this.Controls.Add(box1);
      box1.DataBindings.Add("Text", bindingSource1, "Name");
      view1.Columns[7].DisplayIndex = 0;
  }

  void bindingSource1_CurrentChanged(object sender, EventArgs e)
  {
      box1.Font = (Font)bindingSource1.Current;
  }

Remarks

The Add method refers the call to the underlying list's Add method.

The internal list must contain homogenous types. If the DataSource property has not already been set, then the first object added to the list defines the type for the list.

This method raises the ListChanged event.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also