BindingSource.Add(Object) BindingSource.Add(Object) BindingSource.Add(Object) BindingSource.Add(Object) Method

Définition

Ajoute un élément existant à la liste interne.Adds an existing item to the internal list.

public:
 virtual int Add(System::Object ^ value);
public virtual int Add (object value);
abstract member Add : obj -> int
override this.Add : obj -> int
Public Overridable Function Add (value As Object) As Integer

Paramètres

value
Object Object Object Object

Object à ajouter à la liste interne.An Object to be added to the internal list.

Retours

Index de base zéro auquel value a été ajouté à la liste sous-jacente représentée par la propriété List.The zero-based index at which value was added to the underlying list represented by the List property.

Implémente

Exceptions

value diffère selon les types d'éléments existants dans la liste sous-jacente.value differs in type from the existing items in the underlying list.

Exemples

L’exemple de code suivant illustre l’utilisation Add de la méthode.The following code example how to use the Add method. Pour exécuter cet exemple, collez le code dans un Windows Form et appelez la PopulateBindingSourceWithFonts méthode à partir du constructeur du formulaire.To run this example, paste the code into a Windows Form, and call the PopulateBindingSourceWithFonts method from the form's constructor.

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;
}
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
    Me.Controls.Add(view1)
    box1.Dock = DockStyle.Bottom
    box1.Text = "Sample Text"
    Me.Controls.Add(box1)
    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

Remarques

La Add méthode fait référence à l’appel de la méthode Add de la liste sous-jacente.The Add method refers the call to the underlying list's Add method.

La liste interne doit contenir des types homogènes.The internal list must contain homogenous types. Si la DataSource propriété n’a pas déjà été définie, le premier objet ajouté à la liste définit le type de la liste.If the DataSource property has not already been set, then the first object added to the list defines the type for the list.

Cette méthode déclenche l' ListChanged événement.This method raises the ListChanged event.

S’applique à

Voir aussi