C#之Winform中treeview控件绑定数据库
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控件绑定数据库的更多相关文章
- Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼
Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...
- C#Winform中treeView控件使用总结
1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...
- TreeView控件绑定数据库
1.在设计视图里面的代码 <form id="form1" runat="server"> <div> <h1>两个表< ...
- 040. asp.netWeb中TreeView控件绑定XML文件
xml文件格式: <?xml version="1.0" encoding="utf-8" ?> <sitemap title="进 ...
- asp.net TreeView控件绑定数据库显示信息
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- WinForm 中TreeView 控件的使用实例
新建一个窗体,在本窗体界面上需要以下几个按钮 (一个TreeView 一个 TextBox 三个Button 按钮) 后台代码如下: using System; using System.Co ...
- WinForm中TreeView控件实现鼠标拖动节点(可实现同级节点位置互换,或拖到目标子节点)
;//1:不同级, 不为1:拖同级 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { if (e.Button ...
- [C#]WinForm 中 comboBox控件之数据绑定
[C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...
- Winform中checklistbox控件的常用方法
Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...
随机推荐
- React Native交互组件之Touchable
React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...
- CodeBlocks 中fopen函数不支持命令 “r”
//codeblocks #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp=NULL; if((f ...
- Jrebel6.3.3破解,配置图文教程
JRebel是个很好的开发工具,我在网上找了好久都没有找到很详细的教程,破解与配置教程千篇一律,步骤不详细.编写这篇教程,综合网络上的知识,加上了自己的理解与详细图文步骤. 安装 一般最新的插件是没有 ...
- 腾讯优测优分享 | 这些年,我们追过的 fiddler
腾讯优测是专业的移动云测试平台,提供全面兼容性测试,远程真机租用,漏洞分析等多维度的测试服务,旗下优分享提供大量的移动研发及测试相关的干货! 一.fiddler原理简介 fiddler是目前最强大最好 ...
- Android 开发错误信息001
Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...
- 以ls命令为实例介绍命令基本格式
登陆Linux命令行会显示一行字符,例如[root@localhost ~ ]#, 其中root表示当前登陆用户,localhost表示主机名,~显示的是当前路径,(-表示当前用户的家目录),#表示 ...
- 转--Oracle 审计和测试操作
http://blog.itpub.net/21605631/viewspace-759640/转 Oracle 审计和测试操作 :: 分类: Linux 1.1 相关参数 AUDIT_SYS_OPE ...
- dedecms为后台自定义菜单的完整方法
dedecms为后台自定义菜单的完整方法 品味人生 dedeCMS 围观7330次 18 条评论 编辑日期:2014-06-14 字体:大 中 小 最近在给客户定制一个企业网站,客户要求使用ded ...
- 每日学习心得:Js基本数据类型常用方法扩展
2014-02-17 前言: 节后的第一周上班,整个状态调整的还不错,已完全进入正常的工作状态.这一周主要还是对年前项目存在的一些问题进行完善.修整,基本上没有做大的改动.这里就把项目中经常用到的一些 ...
- SilverlightERP&CRM源码(可用于开发基于Silverlight的CRM,OA,HR,进销存等)
SilverlightERP系统源代码(支持创建OA.SilverlightCRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM Si ...