Question

I have two tables in a DataSet where the ID field on each is the same. I have a Relation between the two tables. How do I, in C# code, pull the info from Table2 that relates to the info on Table1?

I have tried using a new DataRow and assigning it by using GetChildRow, but for some reason I cannot seem to make it work.

Also, I understand this question may not be that informative, let me know and I will try to provide more clarification.

Was it helpful?

Solution

The answer is different (and significantly easier) if your DataSet is strongly-typed (i.e. generated from a .xsd file). I'll assume that's not the case below, but if it is speak up.

For generic DataSet objects, the answer largely depends on what you have hold of to start. If you have simply an ID, then it's probably simplest to use the ID in a select on the relevant DataTable. This will work for either (or both) tables as it will return an array of DataRows with the information you're looking for.

If you have a parent DataRow (and it seems likely that you do), then the best method to use depends on the relationship—i.e. which is the parent. If Table1 is your parent and you want to navigate to relevant Table2 child rows, you're looking for GetChildRow (best to be as specific as you can in telling it which relation to follow). If Table2 is the parent and you're navigating from a Table1 DataRow to the parent in Table2, you'll want to use GetParentRow (again, be as specific in identifying the relation as you can—use the relation object if you have it handy).

OTHER TIPS

In a strongly-typed DataSet, each DataRow object will have specific methods or properties for each relationship. If you have a parent DataRow and want to navigate to children in Table2, it will be a pluralized method (i.e. "row.GetTable2Rows()"). If you have a child and want to navigate to the parent, it will be a singular property ("row.Table2Row").

So here is what I did. Let me know if this is best if or there is something faster. I created a new table that is an exact replica of Table 2 and wrote this code:

currentDataSet.GroupTableOneRow.Clear();
currentDataSet.GroupAttendingEventTable[groupAttendingEventCounter].GetGroupTableRows().CopyToDataTable(currentDataSet.GroupTableOneRow, LoadOption.OverwriteChanges);

Then I can call the one table at record zero and get the name I need. It works, but is there a more efficent way?

A comment wasn't long enough for this response so I'll make it another answer:

There's no need to use a new table. Just use an array of row objects and go from there. Say your DataSet namespace is "EventData" and I'll break it down a bit so it isn't all on one line.

// The parent row
EventData.GroupAttendingEventRow groupAttendingEvent = currentDataSet.GroupAttendingEventTable[groupAttendingEventCounter];
// an array of children rows
EventData.GroupTableRow[] eventGroup = groupAttendingEvent.GetGroupTableRows();

Then you can act on the "eventGroup" array as you would like including getting at the first element with eventGroup[0].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top