找传奇、传世资源到传世资源站!

datagridview结合treeview的应用

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TreeViewTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add("id"); dt.Columns.Add("value"); dt.Columns.Add("dis"); dt.Columns.Add("pre"); dt.Rows.Add(0, "00", "000", 0); dt.Rows.Add(1, "11", "111", 0); dt.Rows.Add(2,"22","222",1); dt.Rows.Add("a", "a3", "a000", "a"); dt.Rows.Add("b", "b2", "b111", "a"); dt.Rows.Add("c", "c1", "c222", "a"); creatTree(dt, treeView1); creatDataGridView(dt, dataGridView1); dataGridView1.RowHeadersVisible = false; treeView1.ItemHeight = dataGridView1.ColumnHeadersHeight 2; treeView1.Indent = 5; tableChange(); } void creatDataGridView(DataTable dt,DataGridView dgv) { dgv.DataSource=dt.DefaultView;} void creatTree(DataTable dt,TreeView tv) { if (dt.Rows.Count == 0) return; foreach (DataRow dr in dt.Rows) {if (dr["id"].ToString() == dr["pre"].ToString()) { tv.Nodes.Add(dr["id"].ToString(), dr["id"].ToString()); } else { TreeNode[] t=tv.Nodes.Find(dr["pre"].ToString(),true); if (t.Length < 1) return; else t[0].Nodes.Add(dr["id"].ToString(), dr["id"].ToString()); } } tv.Nodes.Insert(0, "ID"); tv.Nodes[0].BackColor = Color.Silver; } private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) { tableChange(); } private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e) { tableChange(); } string s; public void GetNode(TreeNodeCollection tc) { foreach (TreeNode TNode in tc) { if (TNode.IsVisible == false) { s = "id<>'" TNode.Name "' and "; } GetNode(TNode.Nodes); } } void tableChange() { s = ""; TreeNodeCollection tc = treeView1.Nodes; GetNode(tc); if(s.Length>4) s = s.Remove(s.Length - 4); dt.DefaultView.RowFilter = s; dataGridView1.DataSource = dt.DefaultView; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复