实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using TreeControlLibrary; using SQLHelper; using System.Threading; using BusinessInterface; using BusinessInstance; using DataMode; namespace TreeViews { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ISimpleInfo Sim = null ; private void Form1_Load( object sender, EventArgs e) { //接口 Sim = LogicInstance.NewInfo(); this .trees.AllowDrop = true ; //是否可以进行拖动 Databinding(); } #region 动态绑定数据 private void BindTree(TreeNodeCollection nodes, DataTable dataSource, int tag) { nodes.Clear(); string filter; filter = string .Format( "{0}={1}" , "ParentID" , tag); DataRow[] drarr = dataSource.Select(filter); TreeNode node; foreach (DataRow dr in drarr) { node = new TreeNode(); node.Text = ( string )dr[ "RegionName" ]; node.Tag = ( int )dr[ "RegionID" ]; node.Name = ( string )dr[ "LCode" ]; nodes.Add(node); BindTree(node.Nodes, dataSource, ( int )node.Tag); } } /// <summary> /// 动态绑定数据 /// </summary> public void Databinding() { string error = string .Empty; //Sim.BindTreeInfo(mytable, 0, "RegionID", "ParentID", "RegionName", "LCode", ref error); DataTable mytable = new DataTable(); mytable = Sim.GetTable( "B_Region" , " and DelFlag=0 order by LCode" ); BindTree(trees.Nodes, mytable, 0); if (error != string .Empty) { MessageBox.Show(error); } trees.ExpandAll(); } #endregion #region 节点操作 /// <summary> /// 节点的添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trees_Add( object sender, TreeClass.ChangeMyArgs e) { string aaa = e.NameInfo; Form f = Application.OpenForms[ "ModifiyNode" ]; //查找是否打开过Form1窗体 if (f == null ) //没打开过 { ModifiyNode fname = new ModifiyNode(); fname.TypeInfo = e.TypeInfo; fname.LCode = e.NameInfo; if (fname.ShowDialog() == DialogResult.OK) { Databinding(); } } else { f.Focus(); //打开过就让其获得焦点 } } /// <summary> /// 节点的删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trees_Del( object sender, TreeClass.ChangeMyArgs e) { string errorinfo = "" ; if (MessageBox.Show( "确定删除?" , "提示" , MessageBoxButtons.YesNo) == DialogResult.Yes) { DataTable myTable = new DataTable(); myTable.TableName = "B_Region" ; myTable = Sim.GetTable( "B_Region" , " and DelFlag=0 and LCode='" e.NameInfo "' order by LCode" ); bool info = Sim.Del(myTable, ref errorinfo); if (info) { MessageBox.Show( "操作成功" ); } else { MessageBox.Show( "操作失败" ); } Databinding(); } } /// <summary> /// 节点的修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trees_Update( object sender, TreeClass.ChangeMyArgs e) { string errorinfo = "" ; string NewName = e.RegionName; DataTable myTable = new DataTable(); myTable = Sim.GetTable( "B_Region" , " and DelFlag=0 and LCode='" e.NameInfo "' order by LCode" ); myTable.TableName = "B_Region" ; myTable.Rows[0][ "RegionName" ] = NewName; //string sqldel = "update B_Region set RegionName='" NewName "' where LCode='" e.NameInfo "'"; if (!Sim.Update(myTable, ref errorinfo)) { MessageBox.Show( "操作失败,请刷新后重试" ); } //Databinding(); } /// <summary> /// 移动节点的保存 /// </summary> /// <param name="NewParentNode">移动最后的新父节点</param> /// <param name="ChildNode">移动节点</param> /// <returns>0失败-1成功</returns> protected int MoveNodes( string NewParentNode, string ChildNode) { string errorinfo = "" ; DataTable mytable = new DataTable(); int ResNum = 0; string FatherLCode = "" ; string HighLCode = "" ; string NewLCode = "" ; //修改后的节点名字:父节点 本级最大节点 string sql = "" ; string FNewCode = "" ; //保存第一次结束以后的父级节点; string NewFaherID; DataRow[] drarr; if (ChildNode != "" ) { DataTable dataSource = new DataTable(); dataSource = SQLHelper.SQLHelper.GetDataTable( "select RegionID,ParentID,RegionCode,RegionName,LCode,DelFlag," "Remark from B_Region where DelFlag=0 and LCode like '" ChildNode "%' order by LCode " ); drarr = dataSource.Select(); NewFaherID = SQLHelper.SQLHelper.ExecuteScalar( "select RegionID from B_Region where LCode='" NewParentNode "'" ).ToString(); //新的父ID FatherLCode = NewParentNode; foreach (DataRow dr in drarr) { DataTable newtable = new DataTable(); //新的开头编号 if (dr[ "LCode" ].ToString() == ChildNode) { try { //第一个节点,改变成当前父节点的最大子节点 HighLCode = SQLHelper.SQLHelper.ExecuteScalar( "select SUBSTRING(LCode,LEN(LCode)-2,3) from B_Region where LCode like '" FatherLCode "___' and LEN(LCode)>3 and DelFlag=0 order by LCode desc" ).ToString(); if (( int .Parse(HighLCode) 1).ToString().Length == 1) { HighLCode = "00" ( int .Parse(HighLCode) 1).ToString(); } else if (( int .Parse(HighLCode) 1).ToString().Length == 2) { HighLCode = "0" ( int .Parse(HighLCode) 1).ToString(); } } catch (Exception) { HighLCode = "001" ; } NewLCode = FNewCode = FatherLCode HighLCode; if (mytable != null ) { newtable = Sim.GetTable( "B_Region" , " and LCode='" dr[ "LCode" ].ToString() "' order by LCode" ); newtable.Rows[0][ "LCode" ] = NewLCode; newtable.Rows[0][ "ParentID" ] = NewFaherID; mytable.Merge(newtable); } else { mytable = Sim.GetTable( "B_Region" , " and LCode='" dr[ "LCode" ].ToString() "' order by LCode" ); } } else { //以后的子节点,只保留最后三位; HighLCode = dr[ "LCode" ].ToString().Substring(dr[ "LCode" ].ToString().Length - 3); NewLCode = FNewCode HighLCode; if (mytable != null ) { newtable = Sim.GetTable( "B_Region" , " and LCode='" dr[ "LCode" ].ToString() "' order by LCode" ); newtable.Rows[0][ "LCode" ] = NewLCode; mytable.Merge(newtable); } else { mytable = Sim.GetTable( "B_Region" , " and LCode='" dr[ "LCode" ].ToString() "' order by LCode" ); } } } ///已经获取完成Datatable,保存操作 try { mytable.TableName = "B_Region" ; //保存 bool info = Sim.Update(mytable, ref errorinfo); if (info) { ResNum = 1; } else { ResNum = 0; } Databinding(); } catch (Exception) { ResNum = 0; } ResNum = 1; } return ResNum; } ///// <summary> ///// 节点当前后面位数更新 ///// </summary> ///// <param name="ChildNode">当前节点</param> ///// <returns></returns> //protected int MoveNodes(string ChildNode) //{ // int ResNum = 0; // string sql = "select LCode from B_Region where LCode like '" ChildNode.Substring(0, ChildNode.Length - 3) "%' and LEN(LCode)>=" ChildNode.Length " and LCode<>'" ChildNode "' and LCode>'" ChildNode "' order by Lcode"; // DataTable MyTable = new DataTable(); // MyTable = SQLHelper.SQLHelper.GetDataTable(sql); // try // { // string FirstCode = MyTable.Rows[0][0].ToString(); // if (FirstCode.Substring(FirstCode.Length - 3, 3) != "001")//当节点需要操作时 才进行操作 // { // foreach (DataRow dr in MyTable.Rows) // { // //string OldCode=dr[] // } // } // } // catch (Exception) // { // ResNum = 1; // } // return ResNum; //} /// <summary> /// 节点的移动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trees_Move( object sender, TreeClass.ChangeMyArgs e) { int Num_First = MoveNodes(e.NameInfo, e.RegionName); if (Num_First == 0) { MessageBox.Show( "操作失败,请刷新后重试" ); } } #endregion } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论