I'm looping through a dataset below and adding some rows to my menuitems.
But what i want to achieve on this line
masterItem.Text = parentItem["Description"].ToString() + "[" +
masterItem.ChildItems.Count.ToString();+ "]";
is to get a count of the ChildRow and append it to the Text property below
For example the output should be:
--Nokia[68]
--Motorola[50]
But when i use masterItem.ChildItems.Count.ToString(); it just returns 0
Is this possible?
Code below
----------------------
DataSet ds =
getMasterDetail_Handset.getAllScheduleMaster_getHandSetsByPhoneCode(PhoneCode);
int hsRowCounts = ds.Tables["HandSets"].Rows.Count;if (hsRowCounts > 0)
{
Menu menu = new Menu();
foreach (DataRow parentItem in ds.Tables["MasterDetails"].Rows)
{
//MenuItem categoryItem = new MenuItem((string)parentItem["Master Code"]);
MenuItem masterItem = new MenuItem();
//categoryItem.ChildItems.Count.ToString();
masterItem.NavigateUrl = "#";
//masterItem.ChildItems.Count.ToString().ToString();
masterItem.Text = parentItem["Description"].ToString() + "[" +
parentItem["Dealer"].ToString() + "]";
menu.Items.Add(masterItem);
foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new
MenuItem((string)childItem["Description"]);childrenItem.NavigateUrl = "#";
masterItem.ChildItems.Add(childrenItem);
}
}
Your code is correct, but a bit out of order. You can't count the child
items until you add them. Here's an example I used based on your code (but
against the Microsoft Northwind database). It loops through the categories
(the MasterItems) and adds each product based on category as child menu items,
then adds the count before it adds into the final Menu object.
Your data logic will be a bit different, but the sequencing of creating the
menu, creating the masterItem, adding the childrenItem(s), then setting the
MasterItem's text should be the same.
NorthwindDataContext db = new NorthwindDataContext();
Menu menu = new Menu();
foreach (Category c in db.Categories.ToList())
{
MenuItem masterItem = new MenuItem();
masterItem.NavigateUrl = "#";
// Loop through the Products table matching CategoryID to
the product's CategoryID.
foreach (Product product in db.Products.Where(x => x.CategoryID
== c.CategoryID))
{
MenuItem childrenItem = new MenuItem(product.ProductName);
childrenItem.NavigateUrl = "#";
masterItem.ChildItems.Add(childrenItem);
}
// After the childItems are added, then count them.
masterItem.Text = string.Format("{0} [{1}]", c.CategoryName,
masterItem.ChildItems.Count.ToString());
menu.Items.Add(masterItem);
}
//form1.Controls.Add(menu);
HTH.
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
> I'm looping through a dataset below and adding some rows to my
> menuitems. But what i want to achieve on this line
[quoted text clipped - 48 lines]
>
> }
Patrick.O.Ige - 08 Apr 2008 22:39 GMT
David thanks for the prompt reply.
I believe you've used linq here in the sample snippet you sent to me
foreach (Product product in db.Products.Where(x => x.CategoryID
> == c.CategoryID))
I did my relationship in the dataset :(
I will try your advice and see how it goes..
By the way i really enjoyed this below from your site..
http://www.tiredstudent.com/blog/files/AJAXingInDotNet.pdf
thumbs up David
> Your code is correct, but a bit out of order. You can't count the child
> items until you add them. Here's an example I used based on your code
[quoted text clipped - 93 lines]
>>
>> }
David R. Longnecker - 09 Apr 2008 14:20 GMT
Thanks for the feedback.
As far as the data source goes, you can use any method to fetch your data
as long as it's enumerable for your foreach statement--even if you build
it by hand out of a DataTable and rows. :) The catch is being sure to count
your MenuItem's ChildItems AFTER you've added the child items. :)
Good luck!
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
> David thanks for the prompt reply.
> I believe you've used linq here in the sample snippet you sent to me
[quoted text clipped - 105 lines]
>>>
>>> }