c#从数据库结果填充树视图
本文关键字:视图 填充 结果 数据库 | 更新日期: 2023-09-27 18:02:16
我正在学习c#编程语言,正在为SAP business One制作工资应用程序附加组件。我以前从未使用过树视图,想知道如何从数据库中填充树项。我使用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
,对于演绎也是如此。我有下面的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();
}
谁能告诉我一个例子,如何填充说收入子树在treeView1?
如果我正确理解了你想要的,那么这里是如何填充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);
}
比起硬编码序数值,我更倾向于获取数据作为XML,然后遍历XML,递归地填充treeview