Groups | Blog | Home
all groups > dotnet ado.net > august 2006 >

dotnet ado.net : Data binding combo box to a datagrid


John Harcourt
8/31/2006 6:17:01 PM
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");
}
PGC
9/1/2006 12:00:00 AM
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

[quoted text, click to view]

John Harcourt
9/1/2006 7:08:02 AM
Paul,

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

John


[quoted text, click to view]
John Harcourt
9/1/2006 7:08:03 AM
Paul,

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

John


[quoted text, click to view]
AddThis Social Bookmark Button