Start New Discussion within our Software Development Community

Help me

I have created a dataset. Then created two data tables with dataset.tables[0] as data for first table and dataset.tables[1] as data for second table. then I created parent and child coloumns. then I created parent row. Now how can I need to select childrows from parentrows.

Regards
jineesh

You have to add relation to the database object.

......
        // Two Datables
        DataTable dt1=new DataTable("Student");
	DataTable dt2=new DataTable("Marks");

        // Student table's columns
        dt1.Columns.Add("Roll",typeof(int));
	dt1.Columns.Add("Name",typeof(string));
	dt1.PrimaryKey=new DataColumn[]{dt1.Columns[0]};
            
        // Marks table's columns
	dt2.Columns.Add("Roll",typeof(int));
	dt2.Columns.Add("Maths",typeof(int));
	dt2.Columns.Add("Eng",typeof(int));
			
	dt1.Rows.Add(new object[]{1,"Rajesh"});
	dt1.Rows.Add(new object[]{2,"Mohan"});
	dt2.Rows.Add(new object[]{1,22,33});
	dt2.Rows.Add(new object[]{2,55,77});
			
	// Create a logical Database - DataSet
	DataSet dst=new DataSet("STDDB");
	dst.Tables.Add(dt1);     // Add the Student table
	dst.Tables.Add(dt2);     // Add the Marks table

	//Adding Relationship
	dst.Relations.Add("Result",dt1.Columns[0],dt2.Columns[0]);
	// Print Rows of parent table and also fetch child rows associated with it.
	for(int i=0;i<dt1.Rows.Count;i++){
		Console.WriteLine(" " + dt1.Rows[i][1]);
		Console.WriteLine("Child Rows : ");
		DataRow []r=dt1.Rows[i].GetChildRows("Result");
			for(int k=0;k<r.Length;k++)	{
			Console.WriteLine("     {0}   {1}    {2}",r[k][0], r[k][1], r[k][2]);
			}
		}
This article has been dead for over six months. Start a new discussion instead.