By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Parent-Child Relation in DataGrid

P: n/a
I have a data grid called Files, which has some columns like FileName, Col1,
Col2, ... , Col8. Then i have a combobox which allows user to select from
Col1 to Col8 and based on this selection, i generate a report from data in
grid Files.
It displays like (e.g. is Col1 is selected):
Col1 Count
----------------------
Val1 x
Val2 y
.....

This data i display in another datagrid called Data. And under each row in
this grid, i want to show related records. So, when i expand row with Val1,
all rows from Files grid which has Val1 as value for Col1 should be displayed.
To achieve this, i created relation between first column (or selected
column, i.e. Col1 here) of the 'Data' grid and the selected column (e.g. Col1
here) of 'Files' grid. Here is the code for the same:
tblData.DataSet.Relations.Add("Details",tblData.Co lumns[cat],tblFiles.Columns[cat])

This works perfectly. Now, the problem is when i change selection in
comboxbox to say Col2 and try to recreate the rows in the 'Data' grid, it
fails.
I used clear method on datatable for it to clear rows, but it gives foreign
key constraint violation says child table has records in it.
So, i tried removing the relation while recreating the datatable by doing:
tblData.DataSet.Relations.Clear()

Doing this, The watch window shows me that relationship is removed, but
still i get the same error. I tried clearing ChildRelations and
ParentRelations as well for the datatable but of no use.
Q1. Am i missing something here? What else i need to do to remove a
relationship?

I did a workaround for this by setting tblData.DataSet.EnforceConstraints =
false. It did the work but I am still clueless how can i remove a
relationship?

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

This is not all... I have some more problems after doing this workaround.. :-(
Now after recreating the 'Data' table, it correctly shows report on the
selected column. And to create different relation now, i do following:
tblData.DataSet.Relations.Clear();
tblData.DataSet.Relations.Add("Details",tblData.Co lumns[cat],tblFiles.Columns[cat]);

However, when i expand one of the rows to see its child rows, it still
refers to old relation. e.g. Now i select Col2 from dropdown, it displays
'Data' as:
Col2 Count
----------------------
Val1 x
Val2 y
.....

If i expand row with Val1, it still shows rows from Files table which has
Val1 as value of Col1 and NOT Col2. Basically last created relationship is
maintained.

I could do a workaround for this as well, by adding everytime relationship
with a new name. So, instead of doing this:
tblData.DataSet.Relations.Add("Details",tblData.Co lumns[cat],tblFiles.Columns[cat]);
i do now:
tblData.DataSet.Relations.Add(cat + "
Details",tblData.Columns[cat],tblFiles.Columns[cat]);

Q2. It works perfectly fine now, but still no idea what is wrong with
original code.

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

Bear with me... There is one more problem now, last one... :-(
Q3. Now on expanding the child rows, i want to get the selected child row.
How to get the selected child row? I am able to find the selected parent row
but have no idea what should be done to get selected child row. Please help.
Thanks for your patience,
Ambica

May 23 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.