BindingSource.Add(Object) 方法

定义

将现有项添加到内部列表中。

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

参数

value
Object

添加到内部列表中的 Object

返回

从零开始的索引,value 按照该索引添加到由 List 属性表示的基础列表中。

实现

例外

value 在类型上与基础列表中的现有项不同。

示例

以下代码示例如何使用 Add 方法。 若要运行此示例,请将代码粘贴到 Windows 窗体中,然后从窗体的构造函数调用 PopulateBindingSourceWithFonts 方法。

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;
  }

注解

方法 Add 引用对基础列表的 方法的 Add 调用。

内部列表必须包含同源类型。 DataSource如果尚未设置 属性,则添加到列表中的第一个对象将定义列表的类型。

此方法引发 ListChanged 事件。

适用于

产品 版本
.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, 10

另请参阅