有一数据库结构及数据如下:
CREATE TABLE [dbo].[eCBMExpense](
[ExpenseID] [varchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ExpenseMC] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ztbz] [char](1) COLLATE Chinese_PRC_CI_AS NOT NULL DEFAULT ('T'),
[ztrq] [smalldatetime] NOT NULL DEFAULT (getdate()),
[userid] [varchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_ECBMEXPENSE] PRIMARY KEY CLUSTERED
(
[ExpenseID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
编号 名称
------ -------------------
01 办公费用
0101 办公费用-纸张费用
010101 办公费用-纸张费用A4
010102 办公费用-纸张费用B5
010103 办公费用-笔墨费用
010104 办公费用-矿泉水费用
02 员工费用
0201 员工费用-出差费用
020101 员工费用-工作餐费用
020102 员工费用-生日费用
020103 员工费用-fdsa
020104 员工费用-t444444
03 其他费用
0301 其他费用-A
030101 其他费用-B
030102 其他费用-C
030103 其他费用-D
030104 其他费用-E
准备数据SQL:
insert into [eCBMExpense]
select '01','办公费用','T',getdate(),'0000'
union all select '0101','办公费用-纸张费用','T',getdate(),'0000'
union all select '010101','办公费用-纸张费用A4','T',getdate(),'0000'
union all select '010102','办公费用-纸张费用B5','T',getdate(),'0000'
union all select '010103','办公费用-笔墨费用','T',getdate(),'0000'
union all select '010104','办公费用-矿泉水费用','T',getdate(),'0000'
union all select '02','员工费用','T',getdate(),'0000'
union all select '0201','员工费用-出差费用','T',getdate(),'0000'
union all select '020101','员工费用-工作餐费用','T',getdate(),'0000'
union all select '020102','员工费用-生日费用','T',getdate(),'0000'
union all select '020103','员工费用-fdsa','T',getdate(),'0000'
union all select '020104','员工费用-t444444','T',getdate(),'0000'
union all select '03','其他费用','T',getdate(),'0000'
union all select '0301','其他费用-A','T',getdate(),'0000'
union all select '030101','其他费用-B','T',getdate(),'0000'
union all select '030102','其他费用-C','T',getdate(),'0000'
union all select '030103','其他费用-D','T',getdate(),'0000'
union all select '030104','其他费用-E','T',getdate(),'0000'
处理方法1:
private void Form1_Load(object sender, EventArgs e)
{
Antho.YYBDAL.DataBase db = new Antho.YYBDAL.DataBase("Data Source=127.0.0.1;Initial Catalog=JcSystemData;User ID=sa;Password=newman2007");
DataTable dt= db.GetDataSetByText("select * from eCBMExpense").Tables[0];
foreach (DataRow row in dt.Rows)
{
string id = Convert.ToString(row["expenseid"]);
if (id.Length == 2)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["expensemc"]);
this.treeView1.Nodes.Add(node);
BindData(id, dt, node);
}
}
}
public void BindData(string parId,DataTable dt,TreeNode parNode)
{
foreach (DataRow row in dt.Rows)
{
string id = Convert.ToString(row["expenseid"]);
if (id.Substring(0, id.Length - 2) == parId)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["expensemc"]);
parNode.Nodes.Add(node);
BindData(id, dt, node);
}
}
}
处理方法2:
DataTable dtTree = new DataTable();
TreeNode Node;
dtTree = dbcc.ExecuteDataSet(CommandType.Text, "select [ExpenseID] as f_id,substring(cast([ExpenseID] as
varchar),1,len([ExpenseID])-2) as f_front,expensemc as f_name from eCBMExpense ").Tables[0];
private void Form1_Load(object sender, EventArgs e)
{
dtTree = dbcc.ExecuteDataSet(CommandType.Text, "select [ExpenseID] as f_id,substring(cast([ExpenseID] as varchar),1,len([ExpenseID])-2) as f_front,expensemc as f_name from eCBMExpense ").Tables[0];
AddTree(" ", (TreeNode)null);
}
public void AddTree(string ParentID, TreeNode pNode)
{
DataView dvTree = new DataView(dtTree); //就是dtTree = dsFrame.Tables[0];
string Fstr = "f_front = '" + ParentID + "'";
dvTree.RowFilter = Fstr;
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null) //处理主节点
{
Node.Name = Row["f_id"].ToString(); //这里+了2个值分别到Name和Text,可随便
Node.Text = Row["f_name"].ToString();
this.treeView1 .Nodes.Add(Node); //加入
AddTree((Row["f_id"].ToString()), Node); //递归
}
else //处理子节点
{
Node.Name = Row["f_id"].ToString();
Node.Text = Row["f_name"].ToString();
pNode.Nodes.Add(Node);
AddTree((Row["f_id"].ToString()), Node);
}
}
}