Data binding combo box with 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. I set up the data binding to a combo
box for the first table. This is the "parent". I set up the data binding for
the datagrid for the second table. This is the child. 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:
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");
}
 
F

Fritz Francis

Hello John,

Are you using .NET 2.0 or .NET 1.1? If you are using .NET 2.0 use two
BindingSource components. So the code would look as follows :

BindingSource bs1 = new BindingSource();
bs1.DataMember = "ParentTableName"
bs1.DataSource = ds

BindingSource bs2 = new BindingSource();
bs2.DataMember = "NameOfRelationship"
bs2.DataSource = bs1

cboName.DisplayMember = "FieldNameToDisplay"
cboName.ValueMember = "FieldNameToHide"
cboName.DataSource = bs1

dataGrid1.DataSource = bs2

I hope this helps.

Regards,

Fritz
 
G

Guest

Sorry, I'm still using 1.1. All that new fangled BindingSource stuff just
causes my head to spin!



Fritz Francis said:
Hello John,

Are you using .NET 2.0 or .NET 1.1? If you are using .NET 2.0 use two
BindingSource components. So the code would look as follows :

BindingSource bs1 = new BindingSource();
bs1.DataMember = "ParentTableName"
bs1.DataSource = ds

BindingSource bs2 = new BindingSource();
bs2.DataMember = "NameOfRelationship"
bs2.DataSource = bs1

cboName.DisplayMember = "FieldNameToDisplay"
cboName.ValueMember = "FieldNameToHide"
cboName.DataSource = bs1

dataGrid1.DataSource = bs2

I hope this helps.

Regards,

Fritz

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. I set up the data binding to a
combo
box for the first table. This is the "parent". I set up the data binding
for
the datagrid for the second table. This is the child. 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:
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