About DataGridView and Binding source

T

Tony Johansson

Hello!

I just wonder what is the difference if I use the code marked with 1 below
compared to using code marked with 2
In the code marked with 1 I use a BindingSource
but in the code marked with 2 I assign a DataTable to the DataSource of the
DataGridView.
So what I can't really understand is why do I have to use a BindingSource in
the code marked with 1.

public partial class Form1 : Form
{
NorthwindDataSet ds = new NorthwindDataSet();
string conStr = @"Data Source=.\SQLExpress;Initial
Catalog=Northwind;Integrated Security=True;";

public Form1()
{
InitializeComponent();

NorthwindDataSetTableAdapters.ProductsTableAdapter productsTA =
new NorthwindDataSetTableAdapters.ProductsTableAdapter();
productsTA.Fill(ds.Products);
1 BindingSource bs = new BindingSource(ds, "Products");
1 myGrid.DataSource = bs;

2 myGrid.DataSource = ds.Tables["Products"];
}
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
Hello!

I just wonder what is the difference if I use the code marked with 1
below
compared to using code marked with 2
In the code marked with 1 I use a BindingSource
but in the code marked with 2 I assign a DataTable to the DataSource of
the
DataGridView.
So what I can't really understand is why do I have to use a BindingSource
in
the code marked with 1. [...]

I don't understand your question. If I read it correctly, you are saying
that it works fine when you don't use a BindingSource. So obviously you
_don't_ have to use a BindingSource.

You should read the documentation regarding binding in .NET. You'll find
that data binding is fairly flexible, accepting a wide variety of types of
sources for data. BindingSource is not mandatory.

Pete

In my example where I have marked with 1 it does not work if I write in this
way see below.
Here I assign a ProductsTableAdapter productsTA to the
DataGridView.DataSource.
But why is it not possible to do it in this way as I have done here ?
public partial class Form1 : Form
{
NorthwindDataSet ds = new NorthwindDataSet();
string conStr = @"Data Source=.\SQLExpress;Initial
Catalog=Northwind;Integrated Security=True;";

public Form1()
{
InitializeComponent();

NorthwindDataSetTableAdapters.ProductsTableAdapter productsTA =
new NorthwindDataSetTableAdapters.ProductsTableAdapter();
productsTA.Fill(ds.Products);
1 BindingSource bs = new BindingSource(ds, "Products");
1 myGrid.DataSource = productsTA;

2 myGrid.DataSource = ds.Tables["Products"];
}
}

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