一个关于取所有子节点的问题 求各位前辈写个方法,比如有张表 部门表 dept(deptid,parentid),就是部门ID 和上一级的ID 我现在要实现下面的方法 我递归不怎么会用 求大家指点 public void getAll(int a){ //得到deptid=a,和他所有子节点的信息}谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Form1_Load(object sender, EventArgs e) { DataTable _Table = new DataTable(); _Table.Columns.Add("DeptID"); _Table.Columns.Add("DeptName"); _Table.Columns.Add("DeptParentID"); _Table.Rows.Add(new object[] {1,"根节电1",0 }); _Table.Rows.Add(new object[] { 2, "根节电2", 0 }); _Table.Rows.Add(new object[] { 3, "子节点1", 1 }); _Table.Rows.Add(new object[] { 4, "子节点2", 1 }); _Table.Rows.Add(new object[] { 5, "子节点3", 1 }); _Table.Rows.Add(new object[] { 6, "子节点A", 2 }); _Table.Rows.Add(new object[] { 7, "子节点B", 2 }); _Table.Rows.Add(new object[] { 8, "子节点C", 2 }); _Table.Rows.Add(new object[] { 9, "子点A", 3 }); _Table.Rows.Add(new object[] { 10, "子点B", 6 }); IList<DataRow> _RowList = new List<DataRow>(); GetDeptRow("1", _Table, _RowList); } public void GetDeptRow(string p_DeptID,DataTable p_DeptTable,IList<DataRow> p_DeptRowList) { DataRow[] _DeptRowList = p_DeptTable.Select("DeptParentID=" + p_DeptID.ToString()); for (int i = 0; i != _DeptRowList.Length; i++) { p_DeptRowList.Add(_DeptRowList[i]); GetDeptRow(_DeptRowList[i]["DeptID"].ToString(), p_DeptTable, p_DeptRowList); } } http://zhidao.baidu.com/question/81358057.html 我要自己读数据库啊 你table是自己加的,而且不太适合我要的方法,谁还有更好的方法 写下 谢谢! C# TreeView中 Node可存储信息 C# LIstView 中获得选中某一项目出现的异常 winfrom中用sql语句改变sql登陆方式 重启服务等===============!!! 电脑待机后,Socket监听不起作用了 c#接口问题 有人会用Flex做一对一的视频聊天吗? 调用Windows Media Format 编辑asf文件问题! DataGridViewCheckBoxColumn列怎么让checkbox禁用掉啊 C#怎么获得内存物理内存? 怎样在WinForm中的各个控件中使用同一种样式 c#两台主机通讯(TCP/IP)判断 C#中怎样调用MFC程序?
private void Form1_Load(object sender, EventArgs e)
{
DataTable _Table = new DataTable();
_Table.Columns.Add("DeptID");
_Table.Columns.Add("DeptName");
_Table.Columns.Add("DeptParentID");
_Table.Rows.Add(new object[] {1,"根节电1",0 });
_Table.Rows.Add(new object[] { 2, "根节电2", 0 });
_Table.Rows.Add(new object[] { 3, "子节点1", 1 });
_Table.Rows.Add(new object[] { 4, "子节点2", 1 });
_Table.Rows.Add(new object[] { 5, "子节点3", 1 }); _Table.Rows.Add(new object[] { 6, "子节点A", 2 });
_Table.Rows.Add(new object[] { 7, "子节点B", 2 });
_Table.Rows.Add(new object[] { 8, "子节点C", 2 });
_Table.Rows.Add(new object[] { 9, "子点A", 3 });
_Table.Rows.Add(new object[] { 10, "子点B", 6 }); IList<DataRow> _RowList = new List<DataRow>();
GetDeptRow("1", _Table, _RowList); } public void GetDeptRow(string p_DeptID,DataTable p_DeptTable,IList<DataRow> p_DeptRowList)
{ DataRow[] _DeptRowList = p_DeptTable.Select("DeptParentID=" + p_DeptID.ToString());
for (int i = 0; i != _DeptRowList.Length; i++)
{
p_DeptRowList.Add(_DeptRowList[i]);
GetDeptRow(_DeptRowList[i]["DeptID"].ToString(), p_DeptTable, p_DeptRowList);
} }