about using BindingSource

T

Tony Johansson

Hello!

This is a simple program that lists all CompanyNames in table Suppliers in a
ComboBox.
When you select a companyName in the ComboBox the corresponding
SupplierID(primary key) is displayed in the label
supplierID.Text
As you can see I have assigned the DataTable myDataSet.Tables[0] to the
DataSource of SupplierList.

Now to my question I can also use a BindingSource in this way.
Remove this line supplierList.DataSource = myDataSet.Tables[0];
and add these two lines myBindingSource = new BindingSource(myDataSet,
"Suppliers");
supplierList.DataSource =
myBindingSource;

So I just wonder when is it important to use a BindingSource as I did here
instead of using supplierList.DataSource = myDataSet.Tables[0];

public Form()
{
SqlDataAdapter myAdapter;
DataSet myDataSet;
BindingSource myBindingSource;

InitializeComponent();

string connectionString = @"Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";

myAdapter = new SqlDataAdapter("select supplierID, companyName from
Suppliers", connectionString);
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");
// myBindingSource = new BindingSource(myDataSet, "Suppliers");
// supplierList.DataSource = myBindingSource;
supplierList.DataSource = myDataSet.Tables[0];
supplierList.DisplayMember = "CompanyName";
supplierList.ValueMember = "SupplierID";
}

private void supplierList_SelectedIndexChanged(object sender, EventArgs e)
{
if (supplierList.SelectedValue != null)
{
supplierID.Text = supplierList.SelectedValue.ToString();
}
}

//Tony
 
M

Morten Wennevik [C# MVP]

Hi Tony,

In your case there may not be much reason to use a BindingSource, but
generally a BindingSource gives you more control over the DataSource,
especially if you are using the same BindingSource bound to several controls,
and BindingSource contains parent-child relationships.

Using a BindingSource you can databind objects very easily. Here is a code
sample demonstrating what you can do with them.

protected override void OnLoad(EventArgs e)
{
ComboBox cbPersons = new ComboBox();
Controls.Add(cbPersons);
ComboBox cbAddresses = new ComboBox();
cbAddresses.Location = new Point(cbPersons.Right + 5, 0);
Controls.Add(cbAddresses);
TextBox tbPerson = new TextBox();
tbPerson.Location = new Point(0, cbPersons.Bottom + 5);
Controls.Add(tbPerson);
TextBox tbAddress = new TextBox();
tbAddress.Location = new Point(cbAddresses.Left, cbAddresses.Bottom + 5);
Controls.Add(tbAddress);

List<Person> people = CreatePeople();
BindingSource bs = new BindingSource(people, "");
BindingSource bsAddress = new BindingSource(bs, "Addresses");
cbPersons.DataSource = bs;
cbPersons.DisplayMember = "Name";
cbAddresses.DataSource = bsAddress;
cbAddresses.DisplayMember = "City";

tbAddress.DataBindings.Add("Text", bsAddress, "Street", false,
DataSourceUpdateMode.OnPropertyChanged);
tbPerson.DataBindings.Add("Text", bs, "Name", false,
DataSourceUpdateMode.OnPropertyChanged);

}

private List<Person> CreatePeople()
{
Person p1 = new Person { Name = "Mom" };
p1.Addresses.Add(new Address { Street = "Sesame Street", City = "MoTown"
});
p1.Addresses.Add(new Address { Street = "Moon Walk", City = "Luna" });
Person p2 = new Person { Name = "Dad" };
p2.Addresses.Add(new Address { Street = "Broadway", City = "Paris" });
p2.Addresses.Add(new Address { Street = "Port", City = "Moresby" });
Person p3 = new Person { Name = "Brother" };
p3.Addresses.Add(new Address { Street = "Some Street", City =
"Chinatown" });
p3.Addresses.Add(new Address { Street = "Back Allay", City = "NoWhere" });

return new List<Person> { p1, p2, p3 };
}

class Person
{
public string Name { get; set; }
public List<Address> Addresses { get; set; }
public Person()
{
Addresses = new List<Address>();
}
}

class Address
{
public string Street { get; set; }
public string City { get; set; }
}


--
Happy Coding!
Morten Wennevik [C# MVP]


Tony Johansson said:
Hello!

This is a simple program that lists all CompanyNames in table Suppliers in a
ComboBox.
When you select a companyName in the ComboBox the corresponding
SupplierID(primary key) is displayed in the label
supplierID.Text
As you can see I have assigned the DataTable myDataSet.Tables[0] to the
DataSource of SupplierList.

Now to my question I can also use a BindingSource in this way.
Remove this line supplierList.DataSource = myDataSet.Tables[0];
and add these two lines myBindingSource = new BindingSource(myDataSet,
"Suppliers");
supplierList.DataSource =
myBindingSource;

So I just wonder when is it important to use a BindingSource as I did here
instead of using supplierList.DataSource = myDataSet.Tables[0];

public Form()
{
SqlDataAdapter myAdapter;
DataSet myDataSet;
BindingSource myBindingSource;

InitializeComponent();

string connectionString = @"Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";

myAdapter = new SqlDataAdapter("select supplierID, companyName from
Suppliers", connectionString);
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");
// myBindingSource = new BindingSource(myDataSet, "Suppliers");
// supplierList.DataSource = myBindingSource;
supplierList.DataSource = myDataSet.Tables[0];
supplierList.DisplayMember = "CompanyName";
supplierList.ValueMember = "SupplierID";
}

private void supplierList_SelectedIndexChanged(object sender, EventArgs e)
{
if (supplierList.SelectedValue != null)
{
supplierID.Text = supplierList.SelectedValue.ToString();
}
}

//Tony
 
T

Tony Johansson

Hello!

I have one question about the example that was sent to me and that is this
part DataSourceUpdateMode.OnPropertyChanged
in this statement
tbAddress.DataBindings.Add("Text", bsAddress, "Street", false,
DataSourceUpdateMode.OnPropertyChanged);
The whole example is below.

Occording to the docs it says Data source is updated whenever the value of
the control property changes.

I don't understand what that mean
Can somebody give me an explanation about this
DataSourceUpdateMode.OnPropertyChanged

//Tony


Morten Wennevik said:
Hi Tony,

In your case there may not be much reason to use a BindingSource, but
generally a BindingSource gives you more control over the DataSource,
especially if you are using the same BindingSource bound to several
controls,
and BindingSource contains parent-child relationships.

Using a BindingSource you can databind objects very easily. Here is a
code
sample demonstrating what you can do with them.

protected override void OnLoad(EventArgs e)
{
ComboBox cbPersons = new ComboBox();
Controls.Add(cbPersons);
ComboBox cbAddresses = new ComboBox();
cbAddresses.Location = new Point(cbPersons.Right + 5, 0);
Controls.Add(cbAddresses);
TextBox tbPerson = new TextBox();
tbPerson.Location = new Point(0, cbPersons.Bottom + 5);
Controls.Add(tbPerson);
TextBox tbAddress = new TextBox();
tbAddress.Location = new Point(cbAddresses.Left, cbAddresses.Bottom +
5);
Controls.Add(tbAddress);

List<Person> people = CreatePeople();
BindingSource bs = new BindingSource(people, "");
BindingSource bsAddress = new BindingSource(bs, "Addresses");
cbPersons.DataSource = bs;
cbPersons.DisplayMember = "Name";
cbAddresses.DataSource = bsAddress;
cbAddresses.DisplayMember = "City";

tbAddress.DataBindings.Add("Text", bsAddress, "Street", false,
DataSourceUpdateMode.OnPropertyChanged);
tbPerson.DataBindings.Add("Text", bs, "Name", false,
DataSourceUpdateMode.OnPropertyChanged);

}

private List<Person> CreatePeople()
{
Person p1 = new Person { Name = "Mom" };
p1.Addresses.Add(new Address { Street = "Sesame Street", City =
"MoTown"
});
p1.Addresses.Add(new Address { Street = "Moon Walk", City = "Luna" });
Person p2 = new Person { Name = "Dad" };
p2.Addresses.Add(new Address { Street = "Broadway", City = "Paris" });
p2.Addresses.Add(new Address { Street = "Port", City = "Moresby" });
Person p3 = new Person { Name = "Brother" };
p3.Addresses.Add(new Address { Street = "Some Street", City =
"Chinatown" });
p3.Addresses.Add(new Address { Street = "Back Allay", City =
"NoWhere" });

return new List<Person> { p1, p2, p3 };
}

class Person
{
public string Name { get; set; }
public List<Address> Addresses { get; set; }
public Person()
{
Addresses = new List<Address>();
}
}

class Address
{
public string Street { get; set; }
public string City { get; set; }
}


--
Happy Coding!
Morten Wennevik [C# MVP]


Tony Johansson said:
Hello!

This is a simple program that lists all CompanyNames in table Suppliers
in a
ComboBox.
When you select a companyName in the ComboBox the corresponding
SupplierID(primary key) is displayed in the label
supplierID.Text
As you can see I have assigned the DataTable myDataSet.Tables[0] to the
DataSource of SupplierList.

Now to my question I can also use a BindingSource in this way.
Remove this line supplierList.DataSource = myDataSet.Tables[0];
and add these two lines myBindingSource = new BindingSource(myDataSet,
"Suppliers");
supplierList.DataSource =
myBindingSource;

So I just wonder when is it important to use a BindingSource as I did
here
instead of using supplierList.DataSource = myDataSet.Tables[0];

public Form()
{
SqlDataAdapter myAdapter;
DataSet myDataSet;
BindingSource myBindingSource;

InitializeComponent();

string connectionString = @"Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";

myAdapter = new SqlDataAdapter("select supplierID, companyName
from
Suppliers", connectionString);
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");
// myBindingSource = new BindingSource(myDataSet, "Suppliers");
// supplierList.DataSource = myBindingSource;
supplierList.DataSource = myDataSet.Tables[0];
supplierList.DisplayMember = "CompanyName";
supplierList.ValueMember = "SupplierID";
}

private void supplierList_SelectedIndexChanged(object sender, EventArgs
e)
{
if (supplierList.SelectedValue != null)
{
supplierID.Text = supplierList.SelectedValue.ToString();
}
}

//Tony
 
T

Tony Johansson

Hello!

I have one question about the example that was sent to me and that is this
part DataSourceUpdateMode.OnPropertyChanged
in this statement
tbAddress.DataBindings.Add("Text", bsAddress, "Street", false,
DataSourceUpdateMode.OnPropertyChanged);
The whole example is below.

Occording to the docs it says Data source is updated whenever the value of
the control property changes.

I don't understand what that mean
Can somebody give me an explanation about this
DataSourceUpdateMode.OnPropertyChanged

//Tony


Morten Wennevik said:
Hi Tony,

In your case there may not be much reason to use a BindingSource, but
generally a BindingSource gives you more control over the DataSource,
especially if you are using the same BindingSource bound to several
controls,
and BindingSource contains parent-child relationships.

Using a BindingSource you can databind objects very easily. Here is a
code
sample demonstrating what you can do with them.

protected override void OnLoad(EventArgs e)
{
ComboBox cbPersons = new ComboBox();
Controls.Add(cbPersons);
ComboBox cbAddresses = new ComboBox();
cbAddresses.Location = new Point(cbPersons.Right + 5, 0);
Controls.Add(cbAddresses);
TextBox tbPerson = new TextBox();
tbPerson.Location = new Point(0, cbPersons.Bottom + 5);
Controls.Add(tbPerson);
TextBox tbAddress = new TextBox();
tbAddress.Location = new Point(cbAddresses.Left, cbAddresses.Bottom +
5);
Controls.Add(tbAddress);

List<Person> people = CreatePeople();
BindingSource bs = new BindingSource(people, "");
BindingSource bsAddress = new BindingSource(bs, "Addresses");
cbPersons.DataSource = bs;
cbPersons.DisplayMember = "Name";
cbAddresses.DataSource = bsAddress;
cbAddresses.DisplayMember = "City";

tbAddress.DataBindings.Add("Text", bsAddress, "Street", false,
DataSourceUpdateMode.OnPropertyChanged);
tbPerson.DataBindings.Add("Text", bs, "Name", false,
DataSourceUpdateMode.OnPropertyChanged);

}

private List<Person> CreatePeople()
{
Person p1 = new Person { Name = "Mom" };
p1.Addresses.Add(new Address { Street = "Sesame Street", City =
"MoTown"
});
p1.Addresses.Add(new Address { Street = "Moon Walk", City = "Luna" });
Person p2 = new Person { Name = "Dad" };
p2.Addresses.Add(new Address { Street = "Broadway", City = "Paris" });
p2.Addresses.Add(new Address { Street = "Port", City = "Moresby" });
Person p3 = new Person { Name = "Brother" };
p3.Addresses.Add(new Address { Street = "Some Street", City =
"Chinatown" });
p3.Addresses.Add(new Address { Street = "Back Allay", City =
"NoWhere" });

return new List<Person> { p1, p2, p3 };
}

class Person
{
public string Name { get; set; }
public List<Address> Addresses { get; set; }
public Person()
{
Addresses = new List<Address>();
}
}

class Address
{
public string Street { get; set; }
public string City { get; set; }
}


--
Happy Coding!
Morten Wennevik [C# MVP]


Tony Johansson said:
Hello!

This is a simple program that lists all CompanyNames in table Suppliers
in a
ComboBox.
When you select a companyName in the ComboBox the corresponding
SupplierID(primary key) is displayed in the label
supplierID.Text
As you can see I have assigned the DataTable myDataSet.Tables[0] to the
DataSource of SupplierList.

Now to my question I can also use a BindingSource in this way.
Remove this line supplierList.DataSource = myDataSet.Tables[0];
and add these two lines myBindingSource = new BindingSource(myDataSet,
"Suppliers");
supplierList.DataSource =
myBindingSource;

So I just wonder when is it important to use a BindingSource as I did
here
instead of using supplierList.DataSource = myDataSet.Tables[0];

public Form()
{
SqlDataAdapter myAdapter;
DataSet myDataSet;
BindingSource myBindingSource;

InitializeComponent();

string connectionString = @"Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";

myAdapter = new SqlDataAdapter("select supplierID, companyName
from
Suppliers", connectionString);
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");
// myBindingSource = new BindingSource(myDataSet, "Suppliers");
// supplierList.DataSource = myBindingSource;
supplierList.DataSource = myDataSet.Tables[0];
supplierList.DisplayMember = "CompanyName";
supplierList.ValueMember = "SupplierID";
}

private void supplierList_SelectedIndexChanged(object sender, EventArgs
e)
{
if (supplierList.SelectedValue != null)
{
supplierID.Text = supplierList.SelectedValue.ToString();
}
}

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top