please help me. I create Query-based Master-detail Relationship without connect to any datagrid etc.. How browse all master records with his child rows. I try foreach rows but childs rows stay for first master row. Sorry I'm C# novice. Thanks for help! Pavel
Code: Select all
private void button1_Click(object sender, EventArgs e)
{
// create Query-based Master-detail Relationship
SqlDataTable deptTable = new SqlDataTable("SELECT * FROM dept", connection);
SqlDataTable empTable = new SqlDataTable("SELECT * FROM emp", connection);
empTable.ParentRelation.ParentTable = deptTable;
empTable.ParentRelation.ParentColumnNames = new string[] { "deptno" };
empTable.ParentRelation.ChildColumnNames = new string[] { "deptno" };
deptTable.Owner = this;
empTable.Owner = this;
deptTable.Open();
empTable.Open();
// browse all master rows with child rows
foreach (DataRow dataRow in deptTable.Rows)
{
// data from master row
foreach (DataColumn dataColumn in deptTable.Columns)
{
richTextBox1.AppendText(dataRow[dataColumn].ToString() + "\t");
}
// data from child rows of current master row stay on first master row
richTextBox1.AppendText(String.Format("empTable.RecordCount: {0} \n\n", empTable.RecordCount));
foreach (DataRow dataRowChild in empTable.Rows)
{
foreach (DataColumn dataColumnChild in empTable.Columns)
{
richTextBox1.AppendText(dataRowChild[dataColumnChild].ToString() + "\t");
}
richTextBox1.AppendText("\n");
}
richTextBox1.AppendText("\n");
}
richTextBox1.AppendText("");
}