如果需要在单元格添加时则用TreeList如果只是单纯读取数据或检索数据时则用GridControl
1.如果点击添加 时则添加TreeList的节点:
protected internal void btnAdd_Click(object sender, EventArgs e) { if (treeList1.Nodes.Count == 0) { TreeListNode node = treeList1.AppendNode(new object[] { 0}, -1); //数组中可写多个参数,但是每一个参数代表一列 } else { int dy = Convert.ToInt32(treeList1.Nodes.LastNode.GetValue("cID")); //获取最后一节点ID TreeListNode node = treeList1.AppendNode(new object[] { dy + 1 }, -1); }
}
2. cPort = treeList1.Nodes[i].GetDisplayText("cPort").ToString().Trim(); //获取单元格中的文本,cport代表列名
bool falge = Convert.ToBoolean(treeList1.Nodes[i].GetValue("cFlage")); //获取复选框的值,cFlage则代表列名
3.删除选中的节点
/// <summary> /// 最后选择的节点 /// </summary> TreeListNode deletenode;
private void treeList1_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { deletenode =e.Node; //获取最后的ID
}
public void btnDel_Click(object sender, EventArgs e) {
this.treeList1.DeleteNode(deletenode); //删除节点
}
4.给节点添加图片
treeListDevice.AppendNode(new object[] { id, type }, 0, 0, 0, 0); //0代表第几张图片代表根目录用的,-1代表子节点图片
5.给节点添加背景颜色
private void treeListConcentration_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e) { string sql = "select 报警信息 from 实时监测数据 where 设备编号='" + nodeID + "'"; DataTable dt = DataHelper.ExecuteDataTable(sql, DataHelper.DataBaseType.属性数据库); if (dt.Rows.Count > 0) { string co = ""; string wornInfo = dt.Rows[0]["报警信息"].ToString();
//string co = wornInfo.Substring(0, 1);//一氧化碳 //string h2s = wornInfo.Substring(1, 1);//硫化氢 //string cl2 = wornInfo.Substring(2, 1);//录气 //string O2 = wornInfo.Substring(3, 1);//氧气 //string hn3 = wornInfo.Substring(4, 1);//氨气 //string pid = wornInfo.Substring(5, 1);//光电离 //string hcl = wornInfo.Substring(5, 1);//椂化氢 string ch4 = wornInfo.Substring(6, 1);//甲烷 if (e.Node[1] != null) { if (e.Node[0].ToString().Trim() == "一氧化碳(ppm)") { message = wornInfo.Substring(0, 1);//一氧化碳 CellBackColor(e, message); } if (e.Node[0].ToString().Trim() == "硫化氢(ppm)") { message = wornInfo.Substring(1, 1);//硫化氢 CellBackColor(e, message); } if (e.Node[0].ToString().Trim() == "氯气(ppm)") { message = wornInfo.Substring(2, 1);//录气 CellBackColor(e, message); } if (e.Node[0].ToString().Trim() == "氧气(VOL%)") { message = wornInfo.Substring(3, 1);//氧气 CellBackColor(e, message); } if (e.Node[0].ToString().Trim() == "氨气(ppm)") { message = wornInfo.Substring(4, 1);//氨气 CellBackColor(e, message); //e.Appearance.BackColor = Color.Red; } if (e.Node[0].ToString().Trim() == "光电离(ppm)") { message = wornInfo.Substring(5, 1);//光电离 CellBackColor(e, message); //e.Appearance.BackColor = Color.Blue; } if (e.Node[0].ToString().Trim() == "氯化氢(ppm)") { message = wornInfo.Substring(6, 1);//椂化氢 CellBackColor(e, message); //e.Appearance.BackColor = Color.Blue; } if (e.Node[0].ToString().Trim() == "甲烷(LEL%)") { message = wornInfo.Substring(7, 1);//甲烷 CellBackColor(e, message); //e.Appearance.BackColor = Color.Blue; }
} }
}
//设置多个颜色
private void CellBackColor(CustomDrawNodeCellEventArgs e, string message) { switch (message) { case "Z"://传感器未打开
e.Appearance.BackColor = Color.Yellow; break; case "I"://传感器通信中断 e.Appearance.BackColor = Color.Blue; break; case "L"://传感器错误 e.Appearance.BackColor = Color.Brown; break; case "A"://传感器阈值报警 e.Appearance.BackColor = Color.Red; break; default: e.Appearance.BackColor = Color.Empty; break; } }
数据源绑定TreeList
//查询设备 public static void selectDevice(TreeList treeDevice) { string sql = "select dID,dName from DeviceMap "; DataTable dt= DataHelper.ExecuteDataTable(sql, DataHelper.DataBaseType.属性数据库); if (dt.Rows.Count>0) { for (int i = 0; i < dt.Rows.Count; i++) { string id = dt.Rows[i]["dID"].ToString(); string type = dt.Rows[i]["dName"].ToString(); treeDevice.AppendNode(new object[] { id,type }, -1); } } }
treeDevice.Columns["dID"].Caption = "类别"; treeDevice.Columns["dType"].Caption = "别名";
注意:必须得绑定字段
//中间插入节点
private void treeList2_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)//单元已有值就会增添一条空信息 { string name = e.Node.GetDisplayText(0); if (name != "") { TreeListNode node = treeList2.AppendNode(new object[] { "" }, -1);
treeList2.SetNodeIndex(node,e.Node.Id+1);
//treeList2.AppendNode(new object[] { "" }, -1); } }
获得选中的节点
nodeID = Convert.ToInt32(treeList1.FocusedNode.GetValue(treeListColumn6));