1

I am learning the C# programming language and am making a payroll application add-on for SAP business One. I have never used a treeview before and wanted to know how one goes about populating the tree items from a database. I am using Visual Studio 2010 and Microsoft SQL Server 2008.

I have one parent with 2 children, i.e.

- Component                        ....Parent
      Earnings                     ....child
      Deductions                   ....child

I want the Earnings child to show all results from a U_PD_description field where U_PD_type = "Earnings", i.e

- Component                        ....Parent
          Earnings                     ....child
              Housing Allowance
              Mobile Phone Allowance
              Mileage Allowance
          Deductions                   ....child

and likewise for deductions. I have the following code for formload:

 private void frm_earn_deduct_setup_Load(object sender, EventArgs e)
        {
            // Get service instance
            var earnDeductMasterService = Program.Kernel.Get<IEarnDeductMasterService>();

            //Query database for all records that have earnings
            var earnings = from ed in earnDeductMasterService.GetAllEarnDeductMasters()
                           where ed.U_PD_type.Trim().Equals("Earnings".Trim(), StringComparison.CurrentCultureIgnoreCase)
                           select ed;

            if (earnings.Any(x => x != null))
            {
                //To populate subtree Earnings with U_PD_description results
                //.....some code here
            }
            else 
            {
                //Nothing to populate            
            }

            //.............................................................................
            //Query database for all records that have deductions
            var deductions = from ed in earnDeductMasterService.GetAllEarnDeductMasters()
                             where ed.U_PD_type.Trim().Equals("Deductions".Trim(), StringComparison.CurrentCultureIgnoreCase)
                             select ed;

            if (deductions.Any(x => x != null))
            {
                //To populate subtree Deductions with U_PD_description results
                //.....some code here
            }
            else
            {
                //Nothing to populate            
            }

            // Disable default items
            txt_amt_greater_than.Enabled = false; 
            bindingNavigatorDeleteItem.Enabled = false;

            // Call service instance
            earnDeductMasterBindingSource.DataSource = Program.Kernel.Get<IEarnDeductMasterService>().GetAllEarnDeductMasters().ToList();
        }

Can anyone show me an example on how to populate say the earnings subtree in treeView1?

2 Answers 2

2

If i understood correctly what you want, then here is example how to populate treeview:

List<string> earnings = new List<string>() { "Housing Allowance", "Mobile Phone Allowance", "Mileage Allowance" };
List<string> deductions = new List<string>() { "Housing Ban", "Mobile Phone Ban", "Mileage Ban" };

treeView1.Nodes.Add("Component");//adding root node
treeView1.Nodes[0].Nodes.Add("Earnings");//adding earnings child node
treeView1.Nodes[0].Nodes.Add("Deductions");//adding deduction child node

//adding all earnings to "Earnings" node
foreach (string earning in earnings)
{
    treeView1.Nodes[0].Nodes[0].Nodes.Add(earning);
}

//adding all deductions to "Deductions" node
foreach (string deduction in deductions)
{
    treeView1.Nodes[0].Nodes[1].Nodes.Add(deduction);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot!! The housing allowance, mobile phone records wont be static. So I guess I'll have to create a list from the results
Yes, you already have them just loop for each and add to teeview node. I just made an example.
0

Instead of hardcoding ordinal values, i would prefer fetching data as XML and then traverse XML, recursively to populate treeview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.