Compartilhar via


BindingSource.Add(Object) Método

Definição

Adiciona um item existente à lista interna.

public:
 virtual int Add(System::Object ^ value);
public virtual int Add (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

Parâmetros

value
Object

Um Object a ser adicionado à lista interna.

Retornos

O índice baseado em zero ao qual value foi adicionado à lista subjacente representada pela propriedade List.

Implementações

Exceções

value difere no tipo dos itens existentes na lista subjacente.

Exemplos

O exemplo de código a seguir como usar o Add método . Para executar este exemplo, cole o código em um Formulário do Windows e chame o PopulateBindingSourceWithFonts método do construtor do formulário.

  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

Comentários

O Add método refere-se à chamada ao método da Add lista subjacente.

A lista interna deve conter tipos homogêneos. Se a DataSource propriedade ainda não tiver sido definida, o primeiro objeto adicionado à lista definirá o tipo da lista.

Esse método gera o ListChanged evento.

Aplica-se a

Confira também