本人按照网上搜的一些小用法整理的:
如果要出现左边的效果,代码如下:
public DataTable
CreateDataTable()
{
DataTable dt = new DataTable();
//此属性设为true
每个节点前会自动增加一个check框
treeList1.OptionsView.ShowCheckBoxes = true;
//此属性设为true 点击复选框时,可设置为半选状态
??效果还未呈现出来
treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
dt.Columns.Add("ID");
dt.Columns.Add("PARENTID");
dt.Columns.Add("NAME");
//为此表增加数据
DataRow dr1 =
dt.NewRow();
dr1["ID"] =
1;
dr1["PARENTID"] =
1;
dr1["NAME"] =
"类目1";
dt.Rows.Add(dr1);
DataRow
dr2 =
dt.NewRow();
dr2["ID"] =
2;
dr2["PARENTID"] =
1;
dr2["NAME"] =
"商品1";
dt.Rows.Add(dr2);
DataRow
dr3 =
dt.NewRow();
dr3["ID"] =
3;
dr3["PARENTID"] =
1;
dr3["NAME"] =
"商品2";
dt.Rows.Add(dr3);
DataRow
dr4 =
dt.NewRow();
dr4["ID"] =
4;
dr4["PARENTID"] =
4;
dr4["NAME"] =
"类目2";
dt.Rows.Add(dr4);
DataRow
dr5 =
dt.NewRow();
dr5["ID"] =
5;
dr5["PARENTID"] =
4;
dr5["NAME"] =
"商品1";
dt.Rows.Add(dr5);
DataRow dr6 =
dt.NewRow();
dr6["ID"] =
6;
dr6["PARENTID"] =
4;
dr6["NAME"] =
"商品2";
dt.Rows.Add(dr6);
this.treeList1.DataSource =
dt;
this.treeList1.KeyFieldName =
"ID";
this.treeList1.ParentFieldName =
"PARENTID";
this.treeList1.Columns[0].Caption = "测试";//树的名称
return
dt;
}
如果要右边的效果,代码是:
public DataTable
CreateTreeList()
{
DataTable dt = new DataTable();
#region TreeList属性设置
//节点前显示复选框
this.treeList2.OptionsView.ShowCheckBoxes = true;
//此属性设为true
点击复选框时,可设置为半选状态
treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
#endregion
#region
在TreeList中增加列
//用于标识当前节点和当前节点的父节点
dt.Columns.Add("ID");
dt.Columns.Add("PARENTID");
//第一列数据的名称
dt.Columns.Add("NAME");
//第二列(节点编码的名称)
dt.Columns.Add("CODE");
//第三列(其他)
dt.Columns.Add("OTHERS");
#endregion
#region 添加数据
//第一行数据
DataRow dr1 =
dt.NewRow();
dr1["ID"] =
1;
dr1["PARENTID"] =
DBNull.Value;
dr1["NAME"] =
"根节点1";
dr1["CODE"] =
"根节点1编码";
dr1["OTHERS"] =
"根节点1的其他信息";
dt.Rows.Add(dr1);
//第二行数据
DataRow dr2 =
dt.NewRow();
dr2["ID"] =
2;
dr2["PARENTID"] =
1;
dr2["NAME"] =
"当前节点2";
dr2["CODE"] =
"当前节点2编码";
dr2["OTHERS"] =
"当前节点2的其他信息";
dt.Rows.Add(dr2);
//第三行数据
DataRow dr3 =
dt.NewRow();
dr3["ID"] =
3;
dr3["PARENTID"] =
1;
dr3["NAME"] =
"当前节点3";
dr3["CODE"] =
"当前节点3编码";
dr3["OTHERS"] =
"当前节点3的其他信息";
dt.Rows.Add(dr3);
//第四行数据
DataRow dr4 =
dt.NewRow();
dr4["ID"] =
4;
dr4["PARENTID"] =
DBNull.Value;
dr4["NAME"] =
"根节点2";
dr4["CODE"] =
"根节点2编码";
dr4["OTHERS"] =
"根节点2的其他信息";
dt.Rows.Add(dr4);
//第五行数据
DataRow dr5 =
dt.NewRow();
dr5["ID"] =
5;
dr5["PARENTID"] =
4;
dr5["NAME"] =
"当前节点5";
dr5["CODE"] =
"当前节点5编码";
dr5["OTHERS"] =
"当前节点5的其他信息";
dt.Rows.Add(dr5);
//第六行数据
DataRow dr6 =
dt.NewRow();
dr6["ID"] =
6;
dr6["PARENTID"] =
4;
dr6["NAME"] =
"当前节点6";
dr6["CODE"] =
"当前节点6编码";
dr6["OTHERS"] =
"当前节点6的其他信息";
dt.Rows.Add(dr6);
#endregion
#region
绑定数据源并设置列名
this.treeList2.DataSource =
dt;
this.treeList2.KeyFieldName =
"ID";
this.treeList2.ParentFieldName = "PARENTID";
//这句一定要写在上面两句的下面
this.treeList2.Columns[0].Caption =
"节点名称";
this.treeList2.Columns[1].Caption =
"节点编码";
this.treeList2.Columns[2].Caption =
"节点其他信息";
#endregion
return
dt;
}