110,536
社区成员
发帖
与我相关
我的任务
分享
private void bindTreeView1()
{
string sql = "select * from dm_category";
DataTable dt = db.ExecuteDataTable(sql, CommandType.Text, null);
DataRow[] dr = dt.Select("parent_id is null");
for (int i = 0; i < dr.Length; i++)
{
TreeNode tn = new TreeNode();
tn.Text = dr[i]["category_name"].ToString();
tn.Tag = dr[i]["id"].ToString();
FillTree(tn, dt);
treeView1.Nodes.Add(tn);
}
}
private void FillTree(TreeNode node, DataTable dt)
{
DataRow[] drr = dt.Select("parent_id='" + node.Tag.ToString() + "'");
if (drr.Length > 0)
{
for (int i = 0; i < drr.Length; i++)
{
TreeNode tnn = new TreeNode();
tnn.Text = drr[i]["category_name"].ToString();
tnn.Tag = drr[i]["id"].ToString();
if (drr[i]["parent_id"].ToString() == node.Tag.ToString())
{
FillTree(tnn, dt);
}
node.Nodes.Add(tnn);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("FInterID", typeof(int));
dt.Columns.Add("FParentID", typeof(int));
dt.Columns.Add("FName", typeof(string));
dt.Columns.Add("FNumber", typeof(string));
dt.Columns.Add("FBootID", typeof(int));
dt.Rows.Add(1038, 0, "3300A", "3300A项目", 1038);
dt.Rows.Add(1039, 1038, "成品组件", "3300A", 1038);
dt.Rows.Add(1040, 1038, "注塑件", "3300A01", 1038);
dt.Rows.Add(1052, 0, "3602U", "3602U项目", 1052);
dt.Rows.Add(1053, 1052, "成品组件", "3602U", 1052);
dt.Rows.Add(1054, 1052, "注塑件", "3602U01", 1052);
dt.Rows.Add(1055, 1052, "涂装件", "3602U02", 1052);
dt.Rows.Add(1056, 1052, "真镀件", "3602U03", 1052);
dt.Rows.Add(1057, 1052, "点胶件", "3602U04", 1052);
dt.Rows.Add(1058, 1052, "冲切件", "3602U05", 1052);
dt.Rows.Add(1059, 1052, "夹具", "3602U17", 1052);
dt.Rows.Add(1060, 0, "3622A", "3622A项目", 1060);
dt.Rows.Add(1061, 1060, "夹具1", "3622A", 1060);
dt.Rows.Add(1062, 1060, "夹具2", "3622A01", 1060);
dt.Rows.Add(1063, 1060, "夹具3", "3622A02", 1060);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (int.Parse(dt.Rows[i]["FParentID"].ToString()) == 0) //如果是根
{
TreeNode root = new TreeNode();
root.Text = dt.Rows[i]["FName"].ToString();
root.Tag = dt.Rows[i]["FInterID"].ToString();
this.treeView1.Nodes.Add(root);
}
else//如果不是根
{
TreeNode root = this.treeView1.Nodes.OfType<TreeNode>().FirstOrDefault(t => (string)t.Tag == dt.Rows[i]["FParentID"].ToString());
if (root != null)
{
TreeNode node = new TreeNode();
node.Text = dt.Rows[i]["FName"].ToString();
node.Tag = dt.Rows[i]["FInterID"].ToString();
root.Nodes.Add(node);
}
}
}
this.treeView1.ExpandAll();//展开tree
}