近日用到了好几个Dev的控件,现总结一下treeList的使用,另外包含一点和GridControl的交互。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace DXWindowsApplication1
{
public partial class TreeListTest : DevExpress.XtraEditors.XtraUserControl
{
public TreeListTest()
{
InitializeComponent();
InitGridControlDatasource();//创建gridcontrol的数据源
CreateTreeListControl();//创建列
CreateTreeNode("产品研发", false);//根节点
string[] Department = new string[5] { "研发总监","研发一部","研发二部","研发三部","研发四部"};//父节点
for (int i = 0; i < Department.Length; i++)
{
CreateChildNode(Department[i], false, 0,"");
}
string[] Phone = { "139****1238", "139****1238", "139****12381", "139****1238", "139****1238" };
string[] Employee = { "部门经理","AA","BB", "CC", "DD" };//子节点
for (int i = 0; i < Employee.Length; i++)
{
CreateChildNode(Employee[i], false, 2,Phone[i]);
}
}
//创建treeList的列
private void CreateTreeListControl()
{
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryItemCheckEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumn = new DevExpress.XtraTreeList.Columns.TreeListColumn();
treeListColumn.Caption = "部门";
treeListColumn.FieldName = "DepartMent";
treeListColumn.Name = "treeListColumn";
treeListColumn.MinWidth = 38;
treeListColumn.Width = 110;
treeListColumn.Visible = true;
treeListColumn.VisibleIndex = 0;
DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumnCheck = new DevExpress.XtraTreeList.Columns.TreeListColumn();
treeListColumnCheck.Caption = "详细资料";
treeListColumnCheck.FieldName = "DetailInfo";
treeListColumnCheck.Name = "treeListColumnCheck";
treeListColumnCheck.MinWidth = 38;
treeListColumnCheck.Width = 80;
treeListColumnCheck.Visible = true;
treeListColumnCheck.ColumnEdit = repositoryItemCheckEdit1;
treeListColumnCheck.VisibleIndex = 1;
DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumnPhone = new DevExpress.XtraTreeList.Columns.TreeListColumn();
treeListColumnPhone.Caption = "联系电话";
treeListColumnPhone.FieldName = "EmployeePhone";
treeListColumnPhone.Name = "treeListColumnPhone";
treeListColumnPhone.MinWidth = 60;
treeListColumnPhone.Width = 120;
treeListColumnPhone.VisibleIndex = 2;
treeListColumnPhone.Visible = true;
this.treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {
treeListColumn,
treeListColumnCheck,
treeListColumnPhone});
this.treeList1.Nodes.Clear();
this.treeList1.Refresh();
}
//创建父节点
private void CreateTreeNode(string Department, bool check)
{
this.treeList1.BeginUnboundLoad();
this.treeList1.AppendNode(new object[] {Department,check}, -1);
this.treeList1.EndUnboundLoad();
}
//创建子节点
private void CreateChildNode(string Name, bool check, int ID,string phone)
{
this.treeList1.BeginUnboundLoad();
this.treeList1.AppendNode(new object[] { Name, check,phone}, ID);
this.treeList1.EndUnboundLoad();
}
//获取父节点
private int getParentID(string ParentNodeName)
{
int parentID = -1;
for (int i = -1; i < this.treeList1.Nodes.Count; i++)
{
if (this.treeList1.Nodes[i][0].ToString() == ParentNodeName)
{
parentID = i;
break;
}
}
return parentID;
}
//主节点选中时,子节点跟随主节点变化
private void treeList1_CellValueChanging(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
{
if (e.Column.Name.Equals("treeListColumnCheck") && e.Node.HasChildren)
{
object objCol = this.treeList1.Columns[1];
for (int i = 0; i < e.Node.Nodes.Count; i++)
{
e.Node.Nodes[i].SetValue(objCol, e.Value);
if (e.Node.Nodes[i].HasChildren)
{
for (int j = 0; j < e.Node.Nodes[i].Nodes.Count; j++)
{
e.Node.Nodes[i].Nodes[j].SetValue(objCol, e.Value);
}
}
}
e.Node.SetValue(objCol, e.Value);
}
}
DataTable dt = new DataTable();
//在这个事件中将单击获得的节点信息添加到gridcontrol的数据源中
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
DataRow row = dt.NewRow() ;
object Department = null;
object Name = null;
object Phone = null;
if (this.treeList1.FocusedNode.ParentNode != null)
{
Department = this.treeList1.FocusedNode.ParentNode.GetValue("DepartMent");
Name = this.treeList1.FocusedNode.GetValue("DepartMent");
Phone = this.treeList1.FocusedNode.GetValue("EmployeePhone");
row["部门"] = Department.ToString();
row["姓名"] = Name.ToString();
row["电话"] = Phone.ToString();
dt.Rows.Add(row);
this.gridControl1.DataSource = dt;
this.gridControl1.Refresh();
}
}
//初始化gridControl的数据源
private void InitGridControlDatasource()
{
DataColumn column1 = new DataColumn();
column1.DataType = System.Type.GetType("System.String");
column1.ColumnName = "部门";
column1.Caption = "部门";
dt.Columns.Add(column1);
DataColumn column2 = new DataColumn();
column2.DataType = System.Type.GetType("System.String");
column2.ColumnName = "姓名";
column2.Caption = "姓名";
dt.Columns.Add(column2);
DataColumn column3 = new DataColumn();
column3.DataType = System.Type.GetType("System.String");
column3.ColumnName = "电话";
column3.Caption = "电话";
dt.Columns.Add(column3);
}
}
}
上述代码基本实现了动态创建treeList,并实现将focusNode的信息传递给gridcontrol去显示。
还有很多不完善的地方,如gridcontrol的数据源会重复添加,应该避免,日后再研究。