Data binding ComboBox, DataGrid

  • Thread starter Thread starter Guest
  • Start date Start date
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");
}
 
Hello John,

The reason why your data grid is not synchronizing is because you have two
currency managers on your form - one for the data table and one for the data
set. You have to specify the same instance as the data source for both. So
change your grid code to:

dataGrid1.DataSource = dt

That way you now only have one currency manager pointing to dt.

I hope this helps.

Regards,

Fritz
 
Back
Top