What is the difference between my two solutions

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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

In your case is the same, as you are only displaying the info.
BindingSource though is much more than that, you can use it for
multiple controls in your form as well as updating the datasource (and
other controls displaying dependand data).
 
T

Tony Johansson

Hello!

What do you mean more exactly when you are saying as well as updating the
datasoure in your answer ?
The DataGridView is just presenting what you have in the datasource(DataSet
in my example)

//Tony


"Ignacio Machin ( .NET/ C# MVP )" <[email protected]> skrev i
meddelandet
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

In your case is the same, as you are only displaying the info.
BindingSource though is much more than that, you can use it for
multiple controls in your form as well as updating the datasource (and
other controls displaying dependand data).
 
B

Ben Voigt [C++ MVP]

I think if you use the BindingSource, then other controls using data-binding
will update whenever you select a record in the grid.
 

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