Data binding combo box to a datagrid

G

Guest

I have a dataset with two data tables. I created a data relation object to
join the two tables by a common field.
On a Windows form, I set up the data binding to a combo
box for the first table. This is the "master". I set up the data binding for
a datagrid for the second table. This is the "detail". When I run it, the
combo box shows me the list in the combo box correctly, and the data grid
shows me the child records for the selected item in the combo box correctly.
But when I change the combo box, the grid does not refresh. I added a label
that is bound to the ID and it does change when the combo changes.

What must I do to get the datagrid to update as the combo box updates?
Thanks for any help!


Code, using the Pubs database:
private void LoadGrid()
{
string sqlText = "select * from authors";
string sqlText2 = "SELECT titleauthor.au_id AS au_id,titles.*
FROM titleauthor LEFT OUTER JOIN titles ON titleauthor.title_id =
titles.title_id ORDER BY titleauthor.au_id";

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlText,
connection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "authors");

dataAdapter = new SqlDataAdapter(sqlText2, connection);
dataAdapter.Fill(ds, "titles");

DataColumn colMaster1 = ds.Tables["authors"].Columns["au_id"];
DataColumn colDetail1 = ds.Tables["titles"].Columns["au_id"];

DataRelation relation = new DataRelation("AuthorTitles",
colMaster1, colDetail1);
ds.Relations.Add(relation);

DataTable dt = ds.Tables["authors"];

cboName.DataSource= dt;
cboName.DisplayMember="au_lname";
cboName.ValueMember ="au_id";

lblID.DataBindings.Add("Text", dt, "au_id");
dataGrid1.SetDataBinding(ds,"authors.AuthorTitles");
}
 
P

PGC

Hi John,

Try binding the datagrid to the "relation". You might want to try using
BindingSource object also. E.g.
Dim myBinding1 as new BindingSource
Dim myBinding2 as new BindingSource

' fill your ds

myBinding1.DataSource = ds
myBinding1.DataMember = ds.<authors table>

myCombo.DataSource = myBinding1
myBinding2.DataSource = myBinding1
myBinding2.DataMember = ds.Relations.Item(<myRelatrion>)
myGrid.DataSource = myBinding2

One way of doing it and obviously not tested but you get the idea.

Paul
 
G

Guest

Paul,

Thanks for the reply. However, I'm still using the 1.1 Framework, not 2.0.

John


PGC said:
Hi John,

Try binding the datagrid to the "relation". You might want to try using
BindingSource object also. E.g.
Dim myBinding1 as new BindingSource
Dim myBinding2 as new BindingSource

' fill your ds

myBinding1.DataSource = ds
myBinding1.DataMember = ds.<authors table>

myCombo.DataSource = myBinding1
myBinding2.DataSource = myBinding1
myBinding2.DataMember = ds.Relations.Item(<myRelatrion>)
myGrid.DataSource = myBinding2

One way of doing it and obviously not tested but you get the idea.

Paul

John Harcourt said:
I have a dataset with two data tables. I created a data relation object to
join the two tables by a common field.
On a Windows form, I set up the data binding to a combo
box for the first table. This is the "master". I set up the data binding
for
a datagrid for the second table. This is the "detail". When I run it, the
combo box shows me the list in the combo box correctly, and the data grid
shows me the child records for the selected item in the combo box
correctly.
But when I change the combo box, the grid does not refresh. I added a
label
that is bound to the ID and it does change when the combo changes.

What must I do to get the datagrid to update as the combo box updates?
Thanks for any help!


Code, using the Pubs database:
private void LoadGrid()
{
string sqlText = "select * from authors";
string sqlText2 = "SELECT titleauthor.au_id AS au_id,titles.*
FROM titleauthor LEFT OUTER JOIN titles ON titleauthor.title_id =
titles.title_id ORDER BY titleauthor.au_id";

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlText,
connection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "authors");

dataAdapter = new SqlDataAdapter(sqlText2, connection);
dataAdapter.Fill(ds, "titles");

DataColumn colMaster1 = ds.Tables["authors"].Columns["au_id"];
DataColumn colDetail1 = ds.Tables["titles"].Columns["au_id"];

DataRelation relation = new DataRelation("AuthorTitles",
colMaster1, colDetail1);
ds.Relations.Add(relation);

DataTable dt = ds.Tables["authors"];

cboName.DataSource= dt;
cboName.DisplayMember="au_lname";
cboName.ValueMember ="au_id";

lblID.DataBindings.Add("Text", dt, "au_id");
dataGrid1.SetDataBinding(ds,"authors.AuthorTitles");
}
 
G

Guest

Paul,

Thanks for the reply. However, I'm still using the 1.1 Framework, not 2.0.

John


PGC said:
Hi John,

Try binding the datagrid to the "relation". You might want to try using
BindingSource object also. E.g.
Dim myBinding1 as new BindingSource
Dim myBinding2 as new BindingSource

' fill your ds

myBinding1.DataSource = ds
myBinding1.DataMember = ds.<authors table>

myCombo.DataSource = myBinding1
myBinding2.DataSource = myBinding1
myBinding2.DataMember = ds.Relations.Item(<myRelatrion>)
myGrid.DataSource = myBinding2

One way of doing it and obviously not tested but you get the idea.

Paul

John Harcourt said:
I have a dataset with two data tables. I created a data relation object to
join the two tables by a common field.
On a Windows form, I set up the data binding to a combo
box for the first table. This is the "master". I set up the data binding
for
a datagrid for the second table. This is the "detail". When I run it, the
combo box shows me the list in the combo box correctly, and the data grid
shows me the child records for the selected item in the combo box
correctly.
But when I change the combo box, the grid does not refresh. I added a
label
that is bound to the ID and it does change when the combo changes.

What must I do to get the datagrid to update as the combo box updates?
Thanks for any help!


Code, using the Pubs database:
private void LoadGrid()
{
string sqlText = "select * from authors";
string sqlText2 = "SELECT titleauthor.au_id AS au_id,titles.*
FROM titleauthor LEFT OUTER JOIN titles ON titleauthor.title_id =
titles.title_id ORDER BY titleauthor.au_id";

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlText,
connection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "authors");

dataAdapter = new SqlDataAdapter(sqlText2, connection);
dataAdapter.Fill(ds, "titles");

DataColumn colMaster1 = ds.Tables["authors"].Columns["au_id"];
DataColumn colDetail1 = ds.Tables["titles"].Columns["au_id"];

DataRelation relation = new DataRelation("AuthorTitles",
colMaster1, colDetail1);
ds.Relations.Add(relation);

DataTable dt = ds.Tables["authors"];

cboName.DataSource= dt;
cboName.DisplayMember="au_lname";
cboName.ValueMember ="au_id";

lblID.DataBindings.Add("Text", dt, "au_id");
dataGrid1.SetDataBinding(ds,"authors.AuthorTitles");
}
 

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