DataRelation, which direction?
I have two related tables:
category and question
Each question has a corresponding category_id which is the (primary) key for the category the question belongs to.
question->category_id == category->id;
If I create a DataRelation between the two tables, does it matter which is the parent table/column and which is the child?
DataRelation MyRelation = new DataRelation("RelationName", MyDataSet.Tables["question"].Columns["category_id"], MyDataSet.Tables["category"].Columns["id"]);
OR
DataRelation MyRelation = new DataRelation("RelationName", MyDataSet.Tables["category"].Columns["id"], MyDataSet.Tables["question"].Columns["category_id"]);
Which is correct? Or will they both work the same way?
Thanks in advance,
Gordon E.
DataRelation MyRelation = new DataRelation("RelationName", PARENT , CHILD ) .
yes the order is important. pk_fk order cannot be inversed.
question->category_id; - fk
category->id; - pk
OK, if I can bother you again, I have a related question... (no pun intended)
I now have three tables, category, question and answer.
Each question has a category (one category TO many questions) and each question has multiple answers (one question TO many answers), here's how I've defined the relationship:
Please assume I've already filled a DataSet called dataSet1 with the "category", "question" and "answer" tables.
I have three DataGridView Controls, one for "category", one for "question" and one for "answer".
I thought I created the relations corretly, because when I click on a row in the "category" DataGridView Control, the correct rows are shown in the "question" DataGridView Control. However, when I click on a row in the "question" DataGridView Control, nothing seems to happen in the "answer" DataGridView Control. I even tried setting the "Nested" property for the DataRelation, but that didn't work.
Could someone post some code that could explain to me how to do this the proper way?
I'm reluctant to post my own code, because it's quite long and complex (because I've created a class to do most of the work).
Any code in C# would be helpful, but if you post a different language I could probably muddle through and figure it out.
Thank you in advance.
Gordon E.
MyDataSet.relations.add("RelationName1", MyDataSet.Tables["category"].Columns["id"], MyDataSet.Tables["question"].Columns["id"])
MyDataSet.relations.add("RelationName2", MyDataSet.Tables["questions"].Columns["id"], MyDataSet.Tables["answer"].Columns["id"])
DataGridView1.DataMember =
"category"DataGridView2.DataMember =
"category.relationname1"DataGridView3.DataMember =
"category.relationname1.RelationName2"
So the last DataMember is based upon "category" even though the relationship is just between questions and answers?
What if I'm using BindingSources?
dataGridView1.DataSource = categoryBS; // etc...
YES! Thank you, thank you, THANK YOU!
I've been struggling with this for a very long time.
I appreciate your help, you don't know how much!
It works, the darn thing actually works!
Now I just have to find the place in the MS library where it explains how to cause primary keys to be generated for new rows that the user inserts.
Thank you again.
Gordon E.