0

I have a dataset contains 2 datatables with a relation between them, in the first table i have an ID (primary key) and MainAreaName and in the second table I have the ID from the first table with 2 other columns

the question is: how to view the MainAreaName from the first table with the other 2 columns from the second table in a gridview

DataSet MyDB = new DataSet("MyDB");
            DataTable MainArea = new DataTable("MainArea");
            DataTable NameNumber = new DataTable("NameNumber");

            DataColumn Col01 = new DataColumn("ID", typeof(int));
            Col01.AutoIncrement = true;
            Col01.AutoIncrementSeed = 1;
            Col01.AutoIncrementStep = 1;
            DataColumn Col02 = new DataColumn("MainAreaName", typeof(string));

            MainArea.Columns.Add(Col01);
            MainArea.Columns.Add(Col02);

            MainArea.PrimaryKey =
                new DataColumn[] { MainArea.Columns["ID"] };


            DataRow MyRow = MainArea.NewRow();
            MyRow["MainAreaName"] = "Area1";
            MainArea.Rows.Add(MyRow);

            MyRow = MainArea.NewRow();
            MyRow["MainAreaName"] = "Area2";
            MainArea.Rows.Add(MyRow);

            DataColumn Col11 = new DataColumn("MainAreaID", typeof(int));
            DataColumn Col12 = new DataColumn("Name", typeof(string));
            DataColumn Col13 = new DataColumn("Number", typeof(int));
            DataColumn Col14 = new DataColumn("Comment", typeof(string));

            NameNumber.Columns.Add(Col11);
            NameNumber.Columns.Add(Col12);
            NameNumber.Columns.Add(Col13);
            NameNumber.Columns.Add(Col14);

            NameNumber.PrimaryKey =
                new DataColumn[] { 
                NameNumber.Columns["MainAreaID"], NameNumber.Columns["Number"] };

            NameNumber.Rows.Add(1, "Test1", 67);
            NameNumber.Rows.Add(1, "Test2", 87);
            NameNumber.Rows.Add(2, "Test3", 77);
            NameNumber.Rows.Add(2, "Test4", 88);

            MyDB.Tables.Add(MainArea);
            MyDB.Tables.Add(NameNumber);

            MyDB.Relations.Add(
                MyDB.Tables["MainArea"].Columns["ID"],
                MyDB.Tables["NameNumber"].Columns["MainAreaID"]);

I tried to use the following code

DataRelation DR = new DataRelation("MainAreaRelation", MyDB.Tables["MainArea"].Columns["ID"], MyDB.Tables["NameNumber"].Columns["MainAreaID"]);
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable; 
//or
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable;

but didn't work, anyone can help please?

  • maybe some sample of your input and expected output will help us – Mauricio Gracia Gutierrez Dec 19 '13 at 21:53
  • 1
    the site prevent me from adding image for the results, my results is just like adding the name from the parent table to view it in the grid view instead of the ID which is the primary key in the parent table that I'm using in the child table to refer to the AreaName – Nancy Ghazal Dec 19 '13 at 22:09
  • possible duplicate of [Inner join of DataTables in C#](http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp) – Zo Has Dec 20 '13 at 06:14
  • Similar thread here http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp. You can use LINQ to join your two tables & fill a 3rd datatable by iterating through the collection http://forums.asp.net/t/1786065.aspx – Zo Has Dec 20 '13 at 06:15

0 Answers0