howto question, regarding Nested Relationships, and getting child counts.

L

Lance Wynn

Suppose I have a self referencing table of categories:

ID
CatName
ParentID

I create a Dataset, and a parent-child relationship, so the result looks
something like this

Cat1
SubCat1
SubCat2
SSubCat1
SSubCat2
SubCat3
Cat2
SubCat1
SubCat2

etc...

I am trying to find an 'elegant' solution to display the count of child rows
next to each parent row, but the count needs to include all child elements,
not just the immediate children, so it would end up looking something like
this:

Cat1 (5)
SubCat1 (0)
SubCat2 (2)
SSubCat1 (0)
SSubCat2 (0)
SubCat3 (0)
Cat2 (2)
SubCat1 (0)
SubCat2 (0)


I've got it working using a seperate array that keeps track as it loops
through the rows, but I am hoping I am just being blind, and there is a
property in the dataset that I can check to get these values.

Lance
 
L

Linda Liu[MSFT]

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.
 
C

Cor Ligthert [MVP]

Linda,

VB is the #1 program language from Microsoft, why are you (as only one)
forever putting C# code in this newsgroup?

Cor
 
L

Lance Wynn

Thanks Linda,
That's actually pretty close to how I was Actually doing it. I was
wondering if there was a better way. I'm pretty new to .Net, (I've been
holding on to VB6 until 2008.) So I wanted to make sure there wasn't a much
better way to do it.

Thanks much.
Lance

Linda Liu said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top