效果:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using WinformTest.BaseServer;
namespace WinformTest
{
public partial class Form1 : Form
{
DataTable dtArea = new DataTable();
public Bitmap cache_bmp;
public Form1()
{
EncryptServerInfor.FileUploadMessage file = new EncryptServerInfor.FileUploadMessage();
file.Bmp = cache_bmp;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Node node1 = new Node();
node1.Name = "0";
node1.Text = "所有区域";
node1.DataKey = null;
node1.TagString = "0";
advTree1.Nodes.Add(node1);
dtArea = new DataTable();
BaseServer.BaseInforClient bll = new BaseServer.BaseInforClient();
Dt_areasInfo model = new Dt_areasInfo() { Category = EnumMakeNumber.dt_areas };
BaseDtoOfDataSetymujubN2 dto = bll.QueryData(BaseServer.ExternalStructEnumOperateObj.查询, model);
if (dto.Code == 200)
{
dtArea = dto.Data.Tables[0];
BindTree(dtArea, node1);
}
//展开
advTree1.ExpandAll();
//关闭
//advTree1.CollapseAll();
}
public void BindTree(DataTable dt, Node node)
{
DataView dv = dt.DefaultView;
dv.RowFilter = " pid=" + node.Name;
DataTable dtnew = dv.ToTable();
if (dtnew.Rows.Count > 0)
foreach (DataRow item in dtnew.Rows)
{
Node nodec = new Node();
nodec.Name = item["id"].ToString();
nodec.Text = "(" + item["id"].ToString() + ")" + item["name"].ToString();
nodec.DataKey = item;
nodec.TagString = item["sort"].ToString();
node.Nodes.Add(nodec);
BindTree(dt, nodec);
}
}
private void advTree1_AfterSelect(object sender, EventArgs e)
{
if (advTree1.SelectedNode != null)
{
MessageBox.Show(advTree1.SelectedNode.Text);
}
}
}
}