C#递归查询地区表绑定树形节点
DAL代码就不贴出来了
BLL代码:
public DataTable DiGui(int ID)
{
SqlParameter[] mySqlParameter =
{
new SqlParameter("@type",SqlDbType.NChar),
new SqlParameter("@Parent_ID",SqlDbType.Int)
};
mySqlParameter[0].Value = "selectArea";
mySqlParameter[1].Value =ID;
return myDALMethod.QueryDataTable("selectArea", mySqlParameter);
}
UIL代码:
public Form1()
{
InitializeComponent();
delegateNewNodes delegateNewNodes = NewNodes;
delegateNewNodes(myBLL.DiGui(0), trv_JieDian.Nodes);
}
BLL myBLL = new BLL();
public delegate void delegateNewNodes(DataTable dt, TreeNodeCollection tnc);//委托
public event delegateNewNodes eventNewNodes;
void NewNodes(DataTable dt, TreeNodeCollection tnc)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = tnc.Add(dr["AREA_NAME"].ToString());
NewNodes(myBLL.DiGui(Convert.ToInt32(dr["ID"])), tn.Nodes);
}
}
存储过程代码:
ALTER PROCEDURE [dbo].[selectArea]
@type varchar(200)='', @Parent_ID int=0
AS
BEGIN
if(@type='selectArea')
begin
SELECT ID, AREA_NAME, PARENT_ID
FROM tbArea
where Parent_ID=@Parent_ID
end
END
数据库表数据
效果: