XtraTreeList 控件使用
DevExpress控件,最近的项目里有用到·功能确实强大,一个控件抵的上以前vs自带控件的几个,算得上是复合控件的。今天来记下XtraTreeList 控件使用,备忘···
1.treeList1绑定数据
- public void InitDate()
- {
- ds = SqlData.QueryDataSet("select deptid,name,code,rootid from dbo.bm_dept");
- treeList1.OptionsBehavior.PopulateServiceColumns = true;
- //this.treeList1.PopulateColumns();
- this.treeList1.ParentFieldName = "rootid";
- this.treeList1.KeyFieldName = "deptid";
- this.treeList1.DataSource = ds.Tables[0];
- }
public void InitDate()
{
ds = SqlData.QueryDataSet("select deptid,name,code,rootid from dbo.bm_dept");
treeList1.OptionsBehavior.PopulateServiceColumns = true;
//this.treeList1.PopulateColumns();
this.treeList1.ParentFieldName = "rootid";
this.treeList1.KeyFieldName = "deptid";
this.treeList1.DataSource = ds.Tables[0];
}
其中的 this.treeList1.ParentFieldName = "rootid";
this.treeList1.KeyFieldName = "deptid";很重要 ParentFieldName 设置的字段是从属于KeyFieldName设置的字段的。如果A数据ParentFieldName 字段等于B数据的KeyFieldName字段,那A是B的子节点,设置成0 那A是根节点·
所以添加新数据时,要注意KeyFieldName字段的选择,确立从属关系··
2.修改时 具备XtraGridControl 列修改的功能
- private void treeList1_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
- {
- try
- {
- string colKey = e.Column.FieldName;
- string sqlStr = "";
- switch (colKey)
- {
- //select deptid,name,code,rootid from bm_dept
- case "deptid":
- sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
- break;
- case "name":
- sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
- break;
- default:
- break;
- }
- if (!sqlStr.Trim().Equals(""))
- SqlData.ExecuteSql(sqlStr);
- InitDate();
- }
- catch (Exception ex)
- {
- XtraMessageBox.Show(ex.Message.ToString());
- }
- }
private void treeList1_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
{
try
{
string colKey = e.Column.FieldName;
string sqlStr = "";
switch (colKey)
{
//select deptid,name,code,rootid from bm_dept
case "deptid":
sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
break;
case "name":
sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
break;
default:
break;
}
if (!sqlStr.Trim().Equals(""))
SqlData.ExecuteSql(sqlStr);
InitDate();
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message.ToString());
}
}
3.删除功能 注意:treeList1.FocusedNode.GetValue("deptid").ToString() 获取行的列值
- private void sBtnDel_Click(object sender, EventArgs e)
- {
- //if(treeList1.FocusedNode != null)
- // treeList1.DeleteNode(treeList1.FocusedNode);
- int deid = int.Parse(treeList1.FocusedNode.GetValue("deptid").ToString());
- SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept");
- while (dr.Read())
- {
- int rtid = int.Parse(dr["rootid"].ToString());
- if ( rtid== deid)
- {
- MessageBox.Show("根节点不能删除!请先删除子节点。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- }
- if (treeList1.FocusedNode != null)
- {
- if (MessageBox.Show("确认删除嘛?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
- {
- try
- {
- int i= SqlData.ExecuteSql("delete from bm_dept where deptid='" + treeList1.FocusedNode.GetValue("deptid").ToString() + "'");
- if (i > 0)
- {
- InitDate();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- else
- { MessageBox.Show("请选择行", "系统提示"); }
- }
private void sBtnDel_Click(object sender, EventArgs e)
{
//if(treeList1.FocusedNode != null)
// treeList1.DeleteNode(treeList1.FocusedNode);
int deid = int.Parse(treeList1.FocusedNode.GetValue("deptid").ToString());
SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept");
while (dr.Read())
{
int rtid = int.Parse(dr["rootid"].ToString());
if ( rtid== deid)
{
MessageBox.Show("根节点不能删除!请先删除子节点。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
if (treeList1.FocusedNode != null)
{
if (MessageBox.Show("确认删除嘛?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
try
{
int i= SqlData.ExecuteSql("delete from bm_dept where deptid='" + treeList1.FocusedNode.GetValue("deptid").ToString() + "'");
if (i > 0)
{
InitDate();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{ MessageBox.Show("请选择行", "系统提示"); }
}
4.TreeList 节点拖放
- string mubiao tuo ;//全局变量
- private void treeList1_DragDrop(object sender, DragEventArgs e)
- {
- DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(new Point(e.X, e.Y)));
- mubiao = hi.Node.GetValue("deptid").ToString();
- if (index == 0)
- {
- //MessageBox.Show("放到当前节点的下级");
- string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", mubiao, tuo);
- SqlData.ExecuteSql(sql);
- }
- else
- {
- //MessageBox.Show("放到当前节点同级");
- SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept where deptid='" + mubiao + "'");
- dr.Read();
- string roots=dr["rootid"].ToString();
- string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", roots, tuo);
- SqlData.ExecuteSql(sql);
- }
- mubiao = "";
- }
- private void treeList1_DragEnter(object sender, DragEventArgs e)
- {
- tuo = treeList1.FocusedNode.GetValue("deptid").ToString();
- }
- private void treeList1_CalcNodeDragImageIndex(object sender, DevExpress.XtraTreeList.CalcNodeDragImageIndexEventArgs e)
- {
- index = e.ImageIndex;
- }
string mubiao tuo ;//全局变量
private void treeList1_DragDrop(object sender, DragEventArgs e)
{
DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(new Point(e.X, e.Y)));
mubiao = hi.Node.GetValue("deptid").ToString();
if (index == 0)
{
//MessageBox.Show("放到当前节点的下级");
string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", mubiao, tuo);
SqlData.ExecuteSql(sql);
}
else
{
//MessageBox.Show("放到当前节点同级");
SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept where deptid='" + mubiao + "'");
dr.Read();
string roots=dr["rootid"].ToString();
string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", roots, tuo);
SqlData.ExecuteSql(sql);
}
mubiao = "";
}
private void treeList1_DragEnter(object sender, DragEventArgs e)
{
tuo = treeList1.FocusedNode.GetValue("deptid").ToString();
}
private void treeList1_CalcNodeDragImageIndex(object sender, DevExpress.XtraTreeList.CalcNodeDragImageIndexEventArgs e)
{
index = e.ImageIndex;
}

浙公网安备 33010602011771号