日期:2014-05-18 浏览次数:21004 次
string strsql = "Select Department from UserInfo group by Department"; DataSet DsforTree = new DataSet(); DsforTree = ObjectMaster.GetDatasetBySql(strsql, "UserInfo");//这是获取DataSet for (int i = 0; i < DsforTree.Tables[0].Rows.Count; i++) { treeView1.Nodes.Add(DsforTree.Tables["UserInfo"].Rows[i]["Department"].ToString()); treeView1.Nodes[i].ImageIndex = 0; string SQLsentence = "Select * from UserInfo where Department='" + treeView1.Nodes[i].Text + "'"; DataSet DsforTreeofaDepartment = new DataSet(); DsforTreeofaDepartment = ObjectMaster.GetDatasetBySql(SQLsentence, "UserInfo"); for (int j = 0; j < DsforTreeofaDepartment.Tables[0].Rows.Count; j++) { treeView1.Nodes[i].Nodes.Add(DsforTreeofaDepartment.Tables["UserInfo"].Rows[j]["nickname"].ToString()); treeView1.Nodes[i].Nodes[j].ImageIndex = 2; } }
------解决方案--------------------
将数据先加载到一个 DataTable 中,这个不需要多说了吧
假设 DataTable 中字段名为 id, dept, name
窗体上有一 TreeView 控件名为 treeView1
下面的代码仅供参考
private void Form1_Load(object sender, EventArgs e) { // 模拟数据加载 DataTable table = new DataTable(); table.Columns.AddRange( new DataColumn[] { new DataColumn("id", typeof(int)), new DataColumn("dept", typeof(string)), new DataColumn("name", typeof(string)) }); table.Rows.Add(new object[] { 1, "财务部", "小红" }); table.Rows.Add(new object[] { 2, "业务部", "小明" }); table.Rows.Add(new obj