treeView 支持一次加入太多数据时太慢了,写了一个分布加入的:
c# 的treeVeiw 前面的"+" "-" 以及线要重画,太慢了,就多写了两个函数,得到下下级,显示“+”
public GoodsSortList(String text)
{
InitializeComponent();
fillTree(null, 0);
fillTree(this.GoodsSortTree.Nodes[0], 1);
}
#region 得到结点的子结点
public void fillTree( TreeNode treenode ,int sortID)
{
DBGoodsSort dbgs = new DBGoodsSort();
GoodsSort gs = new GoodsSort();
gs.sortID = sortID;
DataSet ds = dbgs.getSortlist(gs);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
GoodsSort tmpgs = new GoodsSort();
tmpgs.sortID = Convert.ToInt32(ds.Tables[0].Rows[i]["sortID"]);
tmpgs.parentsortID = Convert.ToInt32(ds.Tables[0].Rows[i]["parentsortID"]);
tmpgs.parentsortPath = ds.Tables[0].Rows[i]["parentsortPath"].ToString();
tmpgs.sortPath = ds.Tables[0].Rows[i]["sortPath"].ToString();
tmpgs.sortCodeSort = ds.Tables[0].Rows[i]["sortCodeSort"].ToString();
tmpgs.sortCodeArray = ds.Tables[0].Rows[i]["sortCodeArray"].ToString();
tmpgs.sortfolderFile = ds.Tables[0].Rows[i]["sortfolderFile"].ToString();
tmpgs.sortName = ds.Tables[0].Rows[i]["sortName"].ToString();
tmpgs.sortTitle = ds.Tables[0].Rows[i]["sortTitle"].ToString();
tmpgs.sortContent = ds.Tables[0].Rows[i]["sortContent"].ToString();
tmpgs.sortTimeInsert = ds.Tables[0].Rows[i]["sortTimeInsert"].ToString();
tmpgs.sortPY = ds.Tables[0].Rows[i]["sortPY"].ToString();
TreeNode tn = new TreeNode(tmpgs.sortName);
tn.Text = tmpgs.sortName;
tn.Name = tmpgs.sortName;
tn.Tag = tmpgs;
if (sortID == 0) // 根结点
{
this.GoodsSortTree.Nodes.Add(tn);
this.GoodsSortTree.ExpandAll();
}
else
{
treenode.Nodes.Add(tn);
treenode.Expand();
}
}
}
#endregion
#region 得到结点的子结点 同时得到他的下级,因为“+”问题
public void fillTreeWithChild(TreeNode treenode, int sortID)
{
DBGoodsSort dbgs = new DBGoodsSort();
GoodsSort gs = new GoodsSort();
gs.sortID = sortID;
DataSet ds = dbgs.getSortlist(gs); // 得到下级的数据
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
GoodsSort tmpgs = new GoodsSort();
tmpgs.sortID = Convert.ToInt32(ds.Tables[0].Rows[i]["sortID"]);
tmpgs.parentsortID = Convert.ToInt32(ds.Tables[0].Rows[i]["parentsortID"]);
tmpgs.parentsortPath = ds.Tables[0].Rows[i]["parentsortPath"].ToString();
tmpgs.sortPath = ds.Tables[0].Rows[i]["sortPath"].ToString();
tmpgs.sortCodeSort = ds.Tables[0].Rows[i]["sortCodeSort"].ToString();
tmpgs.sortCodeArray = ds.Tables[0].Rows[i]["sortCodeArray"].ToString();
tmpgs.sortfolderFile = ds.Tables[0].Rows[i]["sortfolderFile"].ToString();
tmpgs.sortName = ds.Tables[0].Rows[i]["sortName"].ToString();
tmpgs.sortTitle = ds.Tables[0].Rows[i]["sortTitle"].ToString();
tmpgs.sortContent = ds.Tables[0].Rows[i]["sortContent"].ToString();
tmpgs.sortTimeInsert = ds.Tables[0].Rows[i]["sortTimeInsert"].ToString();
tmpgs.sortPY = ds.Tables[0].Rows[i]["sortPY"].ToString();
TreeNode tn = new TreeNode(tmpgs.sortName);
tn.Text = tmpgs.sortName;
tn.Name = tmpgs.sortName;
tn.Tag = tmpgs;
// 得到他是不是有下级
GoodsSort gs1 = new GoodsSort();
gs1.sortID = tmpgs.sortID;
DataSet ds1 = dbgs.getSortlist(gs1);
for (int j = 0; j<ds1.Tables[0].Rows.Count; j++) {
GoodsSort tmpgs1 = new GoodsSort();
tmpgs1.sortID = Convert.ToInt32(ds1.Tables[0].Rows[j]["sortID"]);
tmpgs1.parentsortID = Convert.ToInt32(ds1.Tables[0].Rows[j]["parentsortID"]);
tmpgs1.parentsortPath = ds1.Tables[0].Rows[j]["parentsortPath"].ToString();
tmpgs1.sortPath = ds1.Tables[0].Rows[j]["sortPath"].ToString();
tmpgs1.sortCodeSort = ds1.Tables[0].Rows[j]["sortCodeSort"].ToString();
tmpgs1.sortCodeArray = ds1.Tables[0].Rows[j]["sortCodeArray"].ToString();
tmpgs1.sortfolderFile = ds1.Tables[0].Rows[j]["sortfolderFile"].ToString();
tmpgs1.sortName = ds1.Tables[0].Rows[j]["sortName"].ToString();
tmpgs1.sortTitle = ds1.Tables[0].Rows[j]["sortTitle"].ToString();
tmpgs1.sortContent = ds1.Tables[0].Rows[j]["sortContent"].ToString();
tmpgs1.sortTimeInsert = ds1.Tables[0].Rows[j]["sortTimeInsert"].ToString();
tmpgs1.sortPY = ds1.Tables[0].Rows[j]["sortPY"].ToString();
TreeNode tn1 = new TreeNode(tmpgs1.sortName);
tn1.Text = tmpgs1.sortName;
tn1.Name = tmpgs1.sortName;
tn1.Tag = tmpgs1;
tn.Nodes.Add(tn1);
}
if (sortID == 0) // 根结点
{
this.GoodsSortTree.Nodes.Add(tn);
this.GoodsSortTree.ExpandAll();
}
else
{
treenode.Nodes.Add(tn);
treenode.Expand();
}
}
}
#endregion
public void filechildWithOutThis(TreeNode treenode, int sortID)
{
DBGoodsSort dbgs = new DBGoodsSort();
for (int i = 0; i < treenode.Nodes.Count; i++)
{
if (treenode.Nodes[i].Nodes.Count > 0) { continue; }
GoodsSort gs1 = new GoodsSort();
TreeNode childNode = treenode.Nodes[i];
gs1 = (GoodsSort)childNode.Tag; // 子结点
DataSet ds1 = dbgs.getSortlist(gs1); // 得到子结点
for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
{
GoodsSort tmpgs1 = new GoodsSort();
tmpgs1.sortID = Convert.ToInt32(ds1.Tables[0].Rows[j]["sortID"]);
tmpgs1.parentsortID = Convert.ToInt32(ds1.Tables[0].Rows[j]["parentsortID"]);
tmpgs1.parentsortPath = ds1.Tables[0].Rows[j]["parentsortPath"].ToString();
tmpgs1.sortPath = ds1.Tables[0].Rows[j]["sortPath"].ToString();
tmpgs1.sortCodeSort = ds1.Tables[0].Rows[j]["sortCodeSort"].ToString();
tmpgs1.sortCodeArray = ds1.Tables[0].Rows[j]["sortCodeArray"].ToString();
tmpgs1.sortfolderFile = ds1.Tables[0].Rows[j]["sortfolderFile"].ToString();
tmpgs1.sortName = ds1.Tables[0].Rows[j]["sortName"].ToString();
tmpgs1.sortTitle = ds1.Tables[0].Rows[j]["sortTitle"].ToString();
tmpgs1.sortContent = ds1.Tables[0].Rows[j]["sortContent"].ToString();
tmpgs1.sortTimeInsert = ds1.Tables[0].Rows[j]["sortTimeInsert"].ToString();
tmpgs1.sortPY = ds1.Tables[0].Rows[j]["sortPY"].ToString();
TreeNode tn1 = new TreeNode(tmpgs1.sortName);
tn1.Text = tmpgs1.sortName;
tn1.Name = tmpgs1.sortName;
tn1.Tag = tmpgs1;
childNode.Nodes.Add(tn1);
}
}
}
#region 增加
private void ok_button_Click(object sender, EventArgs e)
{
GoodsSort tmpgs = new GoodsSort();
tmpgs.sortName = this.textBox1.Text;
tmpgs.sortTitle = this.textBox1.Text;
tmpgs.sortContent = this.textBox2.Text.ToString();
tmpgs.sortID = ((GoodsSort)this.currentTreeNode.Tag).sortID;
tmpgs.sortPY = Hz2Py.Convert(tmpgs.sortName);
DBGoodsSort dbgs = new DBGoodsSort();
dbgs.insert(tmpgs);
removeChild(currentTreeNode);
fillTree(currentTreeNode,tmpgs.sortID);
this.textBox1.Text = "";
this.textBox2.Text = "";
}
#endregion
private void GoodsSortTree_AfterSelect(object sender, TreeViewEventArgs e)
{
this.sgs = (GoodsSort)this.GoodsSortTree.SelectedNode.Tag;
this.currentTreeNode = this.GoodsSortTree.SelectedNode;
this.textBox3.Text = this.GoodsSortTree.SelectedNode.Name.ToString();
this.textBox5.Text = this.GoodsSortTree.SelectedNode.Name.ToString();
this.textBox6.Text = this.sgs.sortContent + "";
if (this.sgs.parentsortID != 0)
{
this.textBox4.Text = this.GoodsSortTree.SelectedNode.Parent.Name;
}
}
private void GoodsSortTree_AfterExpand(object sender, TreeViewEventArgs e)
{
this.sgs = (GoodsSort)e.Node.Tag;
this.currentTreeNode = e.Node;
if (this.currentTreeNode.Nodes.Count > 0)
{
filechildWithOutThis(currentTreeNode, this.sgs.sortID);
}
else
{
fillTreeWithChild(currentTreeNode, this.sgs.sortID);
}
}