BindingSource.Add Method (Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Adds an existing item to the internal list.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Overridable Function Add (
	value As Object
) As Integer


Type: System.Object

An Object to be added to the internal list.

Return Value

Type: System.Int32

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

Exception Condition

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

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.

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.

Private WithEvents bindingSource1 As New BindingSource()
Private box1 As New TextBox()

Private Sub PopulateBindingSourceWithFonts()

    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))
    Dim view1 As New DataGridView()
    view1.DataSource = bindingSource1
    view1.AutoGenerateColumns = True
    view1.Dock = DockStyle.Top
    box1.Dock = DockStyle.Bottom
    box1.Text = "Sample Text"
    view1.Columns("Name").DisplayIndex = 0
    box1.DataBindings.Add("Text", bindingSource1, "Name")

End Sub

Sub bindingSource1_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs) _
    Handles bindingSource1.CurrentChanged
    box1.Font = CType(bindingSource1.Current, Font)
End Sub

.NET Framework
Available since 2.0
Return to top