Groups | Blog | Home
all groups > dotnet windows forms databinding > july 2005 >

dotnet windows forms databinding : Howto :: Lookup Databinding :: DataGridView Column :: Foreign key - master - details DataTables


Gordian
7/18/2005 11:44:27 AM
[ I am using VS2005 c# ]

Hello,

I am looking for a description how to use lookup binding in accordance
with a DataGridViewColumn.

I found a nice article about "lookup binding", but i does not use a
DataGridView.

Article
=> Ari Roth's Blog => "Complex Binding and Lookup Binding"
=> http://blogs.msdn.com/ariroth/archive/2005/01/21/358478.aspx


----------------
MORE PRECISELEY

I have a DataGridView which is bound to a DataTable by an BindingSource.
--------
myBindingSource.DataSource = myDataTable_parent;
myDataGridView.DataSource = myBindingSource;
--------

I have two DataTables with these columns:

1) Table => myDataTable_parent ( Name := dtParent )
Columname => ID
Columname => jobtypeID
Columname => some other columns

"jobtypeID" is a foreign-key that relates to the ID-Column of the table
"myDataTable_jobtypeDetails"


2) Table => myDataTable_jobtypeDetails ( Name := dtJobdetails )
Columname => ID
Columname => jobtypename

--------
--------

Analogical to the article mentioned above I tried the following:
=> create a DataSource
=> Add the 2 DataTables and a DataRelation to the DataSet
=> Add a Binding to the DataGridView

--------
myDataset.Tables.Add(myDataTable_parent);
myDataset.Tables.Add(myDataTable_jobstatusDetails);

myDataset.Relations.Add( new DataRelation ("jobtypesRel",
myDataTable_parent["jobtypeID"], myDataTable_jobtypesDetails["ID"]);

myDataGridView.DataBindings.Add("jobtypeID", myDataSet,
"dtJobdetails.jobtypesRel.ID");

--------
--------
PROBLEM

An Argument exception with the following test is thrown during Runtime:

"Cannot bind to the property 'DataGridViewTextBoxColumn {
Name=jobtypeID, Index=0 }' on the target control.
Parameter name: PropertyName"


--------
--------
QUESTION

Can someone give me a description howto Bind a DataGridViewTextBoxColumn
to an DataRelation?

Or another way to resolve the jobtypeID to the jobtypeName?

--------
--------
Thank you for your time.
Greetings Gordian




Gordian
7/19/2005 9:16:43 AM
Hello,
I found some typing errors in my posting.
Here they are:

...............................


Typing error => I found a nice article about "lookup binding", but i
does not use a DataGridView.
Correct => I found a nice article about "lookup binding", but it
does not use a DataGridView.


Typing error => create a DataSource
Correct => create a DataSet

Gordian
7/20/2005 7:51:10 AM
Hello,

I found a solution to display the lookup data from a second DataTable in
a datagridviewcolumn, which is bound to another DataTable.

I found the deciding tip on this page:
=> http://adoguy.com/content.aspx?id=answers/answer&QID=94

There is a short description, to use the
=> DataColumn.Expression Property

to lookup data from a second DataTable via a DataRelation.
And add a new DataColum to the DataTable, which will be bound to
the DataGridView.

(The misconception on my quest to the solution was, to concentrate on
the manipulation of the DataGridView, instead of moving one step back
and use the "root" of my data (the DataTable).

I will post an example, when I have a little bit more time.

Greetings
AddThis Social Bookmark Button