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