Hi Lance,
Since you have created a parent-child relationship on the self referencing
data table, you can call the DataRow.GetChildRows method to get the
immediate child rows of the specified parent data row and then make use of
the recursive algorithm to get the count of all the child rows.
The following is a sample.
public partial class Form1 : Form
{
DataSet ds = new DataSet();
DataTable table = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
// add three data columns into the data table
DataColumn col = new DataColumn("ID", typeof(int));
table.Columns.Add(col);
col = new DataColumn("CatName");
table.Columns.Add(col);
col = new DataColumn("ParentID",typeof(int));
table.Columns.Add(col);
ds.Tables.Add(table);
// add a parent-child relationship into the data set
DataRelation rel = new DataRelation("relation1",
table.Columns[0], table.Columns[2], false);
ds.Relations.Add(rel);
// add some rows into the data table
DataRow row = ds.Tables[0].NewRow();
row[0] = 1;
row[1] = "Cat1";
row[2] = 0;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 2;
row[1] = "SubCat1";
row[2] = 1;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 3;
row[1] = "SubCat2";
row[2] = 1;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 4;
row[1] = "SSubCat1";
row[2] = 3;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 5;
row[1] = "SSubCat2";
row[2] = 3;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 6;
row[1] = "SubCat3";
row[2] = 1;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 7;
row[1] = "Cat2";
row[2] = 0;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 8;
row[1] = "SubCat1";
row[2] = 7;
ds.Tables[0].Rows.Add(row);
row = ds.Tables[0].NewRow();
row[0] = 9;
row[1] = "SubCat2";
row[2] = 7;
ds.Tables[0].Rows.Add(row);
foreach (DataRow datarow in ds.Tables[0].Rows)
{
Console.WriteLine(datarow[0].ToString() + " " +
datarow[1].ToString() + "(" + GetChildCount(datarow).ToString() + ")");
}
}
// get the count of all the child rows of a specified data row
private int GetChildCount(DataRow row)
{
int count = 0;
DataRow[] childrows =
row.GetChildRows(ds.Relations["relation1"]);
foreach (DataRow datarow in childrows)
{
count++;
count += GetChildCount(datarow);
}
return count;
}
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.