// create a new BindingSource object BindingSource myBindingSource = new BindingSource(); // add some items to the list myBindingSource.Add("John Smith"); myBindingSource.Add("Jane Doe"); myBindingSource.Add("Bob Wilson"); // add a new item to the list myBindingSource.AddNew(); // set the value of the first field in the new item myBindingSource[myBindingSource.Count - 1] = "New Item";
// create a new DataTable object DataTable myTable = new DataTable(); // add some columns to the table myTable.Columns.Add("ID", typeof(int)); myTable.Columns.Add("Name", typeof(string)); myTable.Columns.Add("Age", typeof(int)); // create a new BindingSource object and set its DataSource to the table BindingSource myBindingSource = new BindingSource(); myBindingSource.DataSource = myTable; // add a new item to the table DataRow newRow = myTable.NewRow(); newRow["ID"] = 1; newRow["Name"] = "John Smith"; newRow["Age"] = 30; myTable.Rows.Add(newRow); // add a new item to the table using the BindingSource myBindingSource.AddNew(); myBindingSource["ID"] = 2; myBindingSource["Name"] = "Jane Doe"; myBindingSource["Age"] = 25; myBindingSource.EndEdit();In this example, we create a new DataTable object and add some columns to the table. Then, we create a new BindingSource object and set its DataSource to the table. We add a new item to the table using the traditional method of creating a new DataRow object and adding it to the Rows collection. We also add a new item to the table using the AddNew method of the BindingSource object. We set the values of the fields in the new item using the indexer property of the BindingSource object and call the EndEdit method to save the changes. Package library: System.Data.DataTable and System.Windows.Forms.BindingSource