T
Tony Johansson
Hello!
Below I have two solutions that display all customer from the Customers
table of the Northwind databasen.
Solution 1 is using the BindingSource class to assign to the
dataGridViewCustomer.DataSource
Solution 2 is NOT using any BindingSource class. It assign the DataTable
directly to the dataGridViewCustomer.DataSource.
Now to my question that I hope somebody can answer. Why would I ever choose
solution 1 when I can have the same
functionallity with solution 2 that doesn't use any BindingSource
//Solution 1 bind the dataGridView.DataSource to a BindingSource
public Form()
{
InitializeComponent();
thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
custAdapter = new SqlDataAdapter
("Select customerId, Companyname,contactname from customers",
thisConnection);
thisBuilder = new SqlCommandBuilder(custAdapter);
thisDataSet = new DataSet();
custAdapter.Fill(thisDataSet, "Customers");
BindingSource bs = new BindingSource(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = bs;
}
//Solution 2 bind the dataGridView.DataSource direct to a DataTable
public Form()
{
InitializeComponent();
thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
custAdapter = new SqlDataAdapter
("Select customerId, Companyname,contactname from customers",
thisConnection);
thisBuilder = new SqlCommandBuilder(custAdapter);
thisDataSet = new DataSet();
custAdapter.Fill(thisDataSet, "Customers");
BindingSource bs = new BindingSource(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet.Tables[0];
}
//Tony
Below I have two solutions that display all customer from the Customers
table of the Northwind databasen.
Solution 1 is using the BindingSource class to assign to the
dataGridViewCustomer.DataSource
Solution 2 is NOT using any BindingSource class. It assign the DataTable
directly to the dataGridViewCustomer.DataSource.
Now to my question that I hope somebody can answer. Why would I ever choose
solution 1 when I can have the same
functionallity with solution 2 that doesn't use any BindingSource
//Solution 1 bind the dataGridView.DataSource to a BindingSource
public Form()
{
InitializeComponent();
thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
custAdapter = new SqlDataAdapter
("Select customerId, Companyname,contactname from customers",
thisConnection);
thisBuilder = new SqlCommandBuilder(custAdapter);
thisDataSet = new DataSet();
custAdapter.Fill(thisDataSet, "Customers");
BindingSource bs = new BindingSource(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = bs;
}
//Solution 2 bind the dataGridView.DataSource direct to a DataTable
public Form()
{
InitializeComponent();
thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
custAdapter = new SqlDataAdapter
("Select customerId, Companyname,contactname from customers",
thisConnection);
thisBuilder = new SqlCommandBuilder(custAdapter);
thisDataSet = new DataSet();
custAdapter.Fill(thisDataSet, "Customers");
BindingSource bs = new BindingSource(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet.Tables[0];
}
//Tony