我正在学习C#编程语言,正在为SAP business One制作工资单应用程序插件。我以前从未使用过treeview,想知道如何从数据库中填充树项。我正在使用Visual Studio 2010和Microsoft SQL Server 2008。
我有一个有两个孩子的父母,即
- Component ....Parent
Earnings ....child
Deductions ....child
我希望收入子项显示U_PD_description字段的所有结果,其中U_PD_type =“收入”,即
- Component ....Parent
Earnings ....child
Housing Allowance
Mobile Phone Allowance
Mileage Allowance
Deductions ....child
同样适用于扣除。我有以下codeload代码:
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();
}
有人能告诉我一个如何填充说明treeView1中的收益子树的示例吗?
答案 0 :(得分:2)
如果我理解了你想要的东西,那么这里是如何填充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);
}
答案 1 :(得分:0)
我不想硬编码序数值,而是希望以XML格式提取数据然后遍历XML,递归地填充树视图