private void DataShowTree() { this.Cursor = Cursors.WaitCursor; DataTable dt = null; string sSql = " SELECT CONVERT(VARCHAR,ParentID) AS ParentID,convert(varchar,AddressBookID) as KeyID,TypeName as Name FROM tblAddressBookFolder A order by ParentID,Name"; try { dt = sqlHelper.ExecuteDataTable(Common.ConnString, CommandType.Text, sSql); if (dt == null || dt.Rows.Count == 0) { string sSql1 = " INSERT INTO [tblAddressBookFolder]([AddressBookID],[ParentID],[TypeName]) select 1,0,'Data'"; int iResult = sqlHelper.ExecuteNonQuery(Common.ConnString, CommandType.Text, sSql1); dt = sqlHelper.ExecuteDataTable(Common.ConnString, CommandType.Text, sSql); } } catch (Exception ex) { Common.DisplayMsg(this.Text, ex.Message.ToString()); } trl.Nodes.Clear(); trl.DataSource = dt; trl.ParentFieldName = "ParentID"; trl.KeyFieldName = "KeyID"; trl.Columns["Name"].Caption = "通讯录"; // //调用刚才递归并传参,保证所有节点被遍历 //if (!string.IsNullOrEmpty(sFocusedNodeText)) // { // foreach (TreeListNode node in this.trl.Nodes) // { // FindSelectedNode(node); // } // } this.trl.Nodes[0].Expanded = true; // 只显示1级目录 //sSql = "select AddressBookID,TypeName FROM tblAddressBookFolder WHERE AddressBookID>1"; // Common.SetLookUpList(ref loeType, sSql, 0, 1);// 所属关系类别 重新刷一下 this.Cursor = Cursors.Default; }
说明:
1, trl 就是 DevExpress.XtraTreeList.TreeList的 ID
2, 关于 sqlHelper 类,有很多。这里不说了。
3, 数据库表的结构为 tblTest(ParentID int,ID int,Name varchar(20)) 这种格式,3个字段就行了。