private DataSet ds;
private SqlDataAdapter sqlDataAdapter1;
private int maxnodeid; private void Form1_Load(object sender, System.EventArgs e)
{
string strconn=ConfigurationSettings.AppSettings["ConnStr"];
sqlConnection1 = new SqlConnection(strconn);
this.sqlConnection1.Open();
//填充DataSet
this.CreateDataSet();
//从数据库中读取数据,通过递归生成树。
InitTree(this.treeView1.Nodes,""); } private void CreateDataSet()
{
this.sqlDataAdapter1=new SqlDataAdapter("select * from s_menu ",this.sqlConnection1);
this.ds=new DataSet();
this.sqlDataAdapter1.Fill(ds,"tree");
}
private void InitTree(TreeNodeCollection Nds,string parentId)
{
DataView dv=new DataView();
TreeNode tmpNd;
string intId;
dv.Table=ds.Tables["tree"];
dv.RowFilter="ParentId='" + parentId + "'" ;
foreach(DataRowView drv in dv)
{
tmpNd=new TreeNode();
tmpNd.Tag=drv["NodeId"].ToString();
tmpNd.Text=drv["NodeName"].ToString();
Nds.Add(tmpNd);
intId=drv["ParentId"].ToString();
InitTree(tmpNd.Nodes,tmpNd.Tag.ToString());
}
}
//新增节点操作
private void insert(string type)
{//判断是新增树节点,还是子节点.
string strinsert="insert into s_menu values('{0}','{1}','{2}')";
string strformat="";
if(type=="sub")
strformat=string.Format(strinsert,maxnodeid.ToString(),this.selectnode.Tag.ToString(),this.strcomm);
else
strformat=string.Format(strinsert,maxnodeid.ToString(),"",this.strcomm);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
}
//为新增节点算出最大的节点值,并以此值作为新增的节点ID值
private int GetMaxNodeid()
{
int pre=,last=;
DataSet maxds=new DataSet();
this.sqlDataAdapter1=new SqlDataAdapter("select nodeid from s_menu order by nodeid",this.sqlConnection1);
this.sqlDataAdapter1.Fill(maxds);
for(int i=;i{
if(i+{
pre=int.Parse(maxds.Tables[].Rows[i][].ToString());
last=int.Parse(maxds.Tables[].Rows[i+][].ToString());
if(last-pre!=)
return pre+;
}
}
return last+;
}
private void getallnode(TreeNode tn)
{
foreach(TreeNode node in tn.Nodes)
{
list.Add(node.Tag.ToString());
if(node.Nodes.Count>)
{
getallnode(node);
}
}
} private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//判断是否点击了某个节点
this.selectnode= this.treeView1.GetNodeAt (e.X ,e.Y );
if(selectnode==null)
this.isselected=false;
else
this.isselected=true;
}
private void menuAdd_Click(object sender, System.EventArgs e)
{//判断是否点击了某个节点,若没有点击了,则是新增一个树节点
if(isselected==false)
{//算出新增树节点的ID值
maxnodeid=GetMaxNodeid();
TreeNode tmpNd=new TreeNode();
//赋值
tmpNd.Tag=this.maxnodeid.ToString();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{//取到新增树节点的文本值
tmpNd.Text=frmCommon.strcomm;
this.strcomm=frmCommon.strcomm;
//新增树节点
this.treeView1.Nodes.Add(tmpNd);
//插入数据库(说明插入的是树节点)
this.insert("root");
//展开
this.selectnode.Expand();
}
}
else
{//判断是否点击了某个节点,若点击了,则是新增一个子节点
this.contextAddSub();
}
}
private void contextAddSub()
{//得到新增子节点的ID值
maxnodeid=GetMaxNodeid();
TreeNode tmpNd=new TreeNode();
//赋值
tmpNd.Tag=this.maxnodeid.ToString();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{//取到新增树节点的文本值
tmpNd.Text=frmCommon.strcomm;
this.strcomm=frmCommon.strcomm;
//新增子节点
this.selectnode.Nodes.Add(tmpNd);
//插入数据库(说明插入的是子节点)
this.insert("sub");
//展开
this.treeView1.SelectedNode.Expand();
}
}
//删除节点操作
private void menuDel_Click(object sender, System.EventArgs e)
{//新建一个ArrayList,用于保存要删除的节点下边的所有子节点
list=new ArrayList();
if(this.isselected==true)
{//得到删除的节点下边的所有子节点
getallnode(this.selectnode);
//把要删除的节点也加进去
list.Add(this.selectnode.Tag.ToString());
//循环从数据库中删除
for(int i=;i{
string strdel="delete s_menu where nodeid='{0}'";
string strformat="";
strformat=string.Format(strdel,list[i]);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
}
//从树中删除
this.selectnode.Remove();
}
}
//修改节点的值
private void menuEdit_Click(object sender, System.EventArgs e)
{
if(this.isselected==true)
{
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{
string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";
string strformat="";
strformat=string.Format(strdel,this.selectnode.Tag.ToString(),frmCommon.strcomm);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
this.selectnode.Text=frmCommon.strcomm;
}
}
}
//遍历所有节点.查找值
private void getvaluenode(TreeNodeCollection tn,string value)
{
foreach(TreeNode node in tn)
{
if(node.Nodes.Count>)
{
getvaluenode(node.Nodes,value);
}
if(node.Text==value)
listnode.Add(node);
}
}
private void menuSearch_Click(object sender, System.EventArgs e)
{
int j,k;
this.listnode=new ArrayList();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{
TreeNode n =new TreeNode();
TreeNode temp=new TreeNode();
//下面的函数是填充listnode;
getvaluenode(this.treeView1.Nodes,frmCommon.strcomm);
for(int i=;i{
j=;k=;
n=(TreeNode)listnode[i];
if (n != null)
{
temp=n;
//得到上面结点的数量,并将数量保存到变量j;
for(;n.Parent!=null;)
{
n=n.Parent;
j++;
}
//恢复原值
n=temp;
//新建一个树结点数组做保存得到查询到的所有节点.
TreeNode[] m=new TreeNode[j];
for(;n.Parent!=null;)
{
n=n.Parent;
m[k]=n;
k++;
}
for(int p=;pm[p].Expand();
n=temp;
n.ForeColor=Color.Red;
}
}
}
}
private void treeView1_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
if(this.treeView1.SelectedNode.Text!=null)
{
string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";
string strformat="";strformat=string.Format(strdel,this.treeView1.SelectedNode.Tag.ToString(),e.Label.ToString());SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery(); }
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
this.listBox1.Items.Clear();
this.listBox1.Items.Add(this.treeView1.SelectedNode.FullPath.ToString());
}
}
}

C#之Winform中treeview控件绑定数据库的更多相关文章

  1. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  2. C#Winform中treeView控件使用总结

    1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...

  3. TreeView控件绑定数据库

    1.在设计视图里面的代码 <form id="form1" runat="server"> <div> <h1>两个表< ...

  4. 040. asp.netWeb中TreeView控件绑定XML文件

    xml文件格式: <?xml version="1.0" encoding="utf-8" ?> <sitemap title="进 ...

  5. asp.net TreeView控件绑定数据库显示信息

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. WinForm 中TreeView 控件的使用实例

    新建一个窗体,在本窗体界面上需要以下几个按钮 (一个TreeView    一个 TextBox  三个Button 按钮) 后台代码如下: using System; using System.Co ...

  7. WinForm中TreeView控件实现鼠标拖动节点(可实现同级节点位置互换,或拖到目标子节点)

    ;//1:不同级, 不为1:拖同级 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { if (e.Button ...

  8. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  9. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

随机推荐

  1. 跟大牛之间关于hibernate的一些探讨记录

    hibernate的工作原理!! 1.读取配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transcation 5.持久化操作 6.提交事务 ...

  2. Android ViewPager Fragment使用懒加载提升性能

     Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...

  3. JDK各版本新特性!

    1.JDK1.5 新特性 1.自动装箱与拆箱:自动装箱的过程:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中.自动拆箱的过程:每当需要一个值时,被装箱对象中的值就被自动地提取 ...

  4. Centos7.2 Systemd 方式编译 Mysql5.7.11

    导读 MySQL 5.7 版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 原生支持Systemd 更好的性能:对于多核CPU. ...

  5. Objective-c——UI进阶开发第一天(UIPickerView和UIDatePicker)

    一.知识点 1.介绍数据选择控件UIPickerView和日期选择控件UIDatePicker控件 * UIPickerView的案例 * 点餐系统 * 城市选择 * 国旗选择 * UIDatePic ...

  6. crontabs Permission denied

    问题 jack@somemachine /data/jack $ crontab -e crontabs/jack: Permission denied 解决办法 sudo chown root:cr ...

  7. kuangbin_SegTree E (HDU 1698)

    POJ服务器炸了 还没好呢 然后就只能跳掉一些题目了 这题也是成段更新模板题 本来lazy标记不是很明白 后来学长上课讲了一下就知道原理了 回去看看代码很容易就理解了 #include <cst ...

  8. MySQL物理文件组成

    日志文件 错误日志:Error Log 错误日志记录了MySQL运行过程中所有较为严重的警告和错误信息,以及MySQL Server每次启动和关闭的详细信息.在默认情况下,系统记录错误日志的功能是关闭 ...

  9. SQL2005 遍历表插入

    /* sql2005遍历表(方法1) insert into 数据表(userid,adddate) values((select userid from 用户表),date); */ /*sql20 ...

  10. H5课程大纲

    K1模块课程: 课程模块 课程阶段 课程内容 K1 模块 第1阶段 认识前端开发 环境配置.使用标签的分类.写法及使用规范CSS样式的使用.各类常见样式Photoshop使用16大常用样式盒模型.语义 ...