how to reflect parent id from parent table to child table in c#

1k views Asked by At

Trying to get parent id directly in child table , Is it possible to get parent id in child table by just using dsTest.Relations.Add() also tried by adding cascade.for cascade How to update Dataset Parent & Child tables with Autogenerated Identity Key?

DataSet dsTest = new DataSet("DataSet");

       DataTable parentTable = new DataTable("Parents");

        parentTable.Columns.Add("ParentId", typeof(int));

        parentTable.Columns.Add("ParentName", typeof(string));

        //Create some parents.

        parentTable.Rows.Add(new object[] { 1, "Parent # 1" });

        parentTable.Rows.Add(new object[] { 2, "Parent # 2" });

        parentTable.Rows.Add(new object[] { 3, "Parent # 3" });

        dsTest.Tables.Add(parentTable);


        // Table for childrend

        DataTable childTable = new DataTable("Childs");

        childTable.Columns.Add("ChildId", typeof(int));

        childTable.Columns.Add("ChildName", typeof(string));

        childTable.Columns.Add("ParentId", typeof(int));

        //Create some childs.

        childTable.Rows.Add(new object[] { 1, "Child # 1", });

        childTable.Rows.Add(new object[] { 2, "Child # 2", });

        childTable.Rows.Add(new object[] { 3, "Child # 3", });

        childTable.Rows.Add(new object[] { 4, "Child # 4", });

        childTable.Rows.Add(new object[] { 5, "Child # 5", });

        dsTest.Tables.Add(childTable);

        //// Create their relation.

        DataRelation parentChildRelation = new DataRelation("ParentChild", parentTable.Columns["ParentId"], childTable.Columns["ParentId"]);

        dsTest.Relations.Add(parentChildRelation);

        childTable.AcceptChanges();
0

There are 0 answers