⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectpc.cs

📁 机械制造业信息管理系统(含源码) 是为一个粮仪厂开发的
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Data;
using System.Data.OleDb;

namespace Eboer.MIS.MF.WinForm
{
	/// <summary>
	/// SelectPC 的摘要说明。
	/// </summary>
	public class SelectPC : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ColumnHeader _gg;
		private System.Windows.Forms.ColumnHeader _pn;
		private System.Windows.Forms.ColumnHeader _unit;
		private System.Windows.Forms.ColumnHeader _price;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TreeView treeView;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ColumnHeader _id;
		private System.Windows.Forms.ListView listView;

		private DataSet ds = null;

		public object  _object  = null;

		public SelectPC(object _object)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			this._object = _object;

			this.treeView.MouseDown += new MouseEventHandler(ClickInTreeView);

			this.LoadTree();

			//载入原有数据
			this.LoadOldData();
		}

		/// <summary>
		/// 载入时首先要获取原始窗口中的数据
		/// </summary>
		private void LoadOldData(){

			//父窗品中的DataGrid的数据源
			DataGrid dg = this.GetParentDG();
			if(dg == null) return;

			DataTable dt = ((DataTable)dg.DataSource);

			for(int i=0;i<dt.Rows.Count;i++){
				if(!dt.Rows[i][7].ToString().Trim().Equals("")){
					this.OldDataToTree(this.treeView.Nodes[0],dt.Rows[i][7].ToString().Trim(),dt.Rows[i]);
				}
			}
		}

		private void OldDataToTree(TreeNode node,string bh,DataRow row){
			for(int i=0;i<node.Nodes.Count;i++){

				if(node.Nodes[i].Tag.ToString().Equals(bh)){

					//选中树中的符合条件的节点
					node.Nodes[i].Checked = true;
								
					//加入列表中
					ListViewItem li = new ListViewItem(new string[]{row[7].ToString(),row[1].ToString(),row[0].ToString(),row[3].ToString(),row[4].ToString()});
					this.listView.Items.Add(li);
				}

				if(node.Nodes[i].Nodes.Count > 0){
					this.OldDataToTree(node.Nodes[i],bh,row);
				}
			}
		}

		/// <summary>
		/// 返回父窗的DG
		/// </summary>
		/// <returns></returns>
		private DataGrid GetParentDG(){
			DataGrid dg = null;
			if(this._object.GetType().Namespace.ToString().EndsWith(".Sell")){ //销售单
				dg  = ((Eboer.MIS.MF.WinForm.Sell.EditOrder)this._object).list;
			}else{
				dg  = ((Eboer.MIS.MF.WinForm.Stock.EditOrder)this._object).list;
			}
			return dg;
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 载入树结构
		/// </summary>
		private void LoadTree(){

			try{
				string sql = "select * from ProductCatalog order by treeID asc";

				OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);

				this.ds = new DataSet();
				ad.Fill(ds,"tree");

				this.treeView.Nodes.Clear();
				this.CreateTree(0,null);

				this.treeView.CheckBoxes = true;

			}catch(Exception ex){
				MessageBox.Show(ex.Message.ToString());
				return;
			}
		}

		/// <summary>
		/// 递归添加树的节点
		/// </summary>
		/// <param name="parentID"></param>
		/// <param name="pNode"></param>
		public void CreateTree(int parentID,TreeNode pNode){
			DataView dvTree = new DataView(this.ds.Tables[0]);
			dvTree.RowFilter = "[PARENTID]="+parentID;
			foreach(DataRowView row in dvTree){
				if(pNode == null){ //根结点
					TreeNode treeNode = this.treeView.Nodes.Add(row["signName"].ToString());
					treeNode.Tag = row["treeID"].ToString();
					treeNode.ExpandAll();
					treeNode.ImageIndex = 0;
					this.CreateTree(Int32.Parse(row["autoID"].ToString()),treeNode);
				}else{
					TreeNode treeNode = pNode.Nodes.Add(row["gg"].ToString() + " " + row["SignName"].ToString());
					treeNode.Tag = row["treeID"].ToString();
					this.CreateTree(Int32.Parse(row["autoID"].ToString()),treeNode);
				}
			}
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.listView = new System.Windows.Forms.ListView();
			this._gg = new System.Windows.Forms.ColumnHeader();
			this._pn = new System.Windows.Forms.ColumnHeader();
			this._unit = new System.Windows.Forms.ColumnHeader();
			this._price = new System.Windows.Forms.ColumnHeader();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.treeView = new System.Windows.Forms.TreeView();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this._id = new System.Windows.Forms.ColumnHeader();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panel1.Controls.Add(this.listView);
			this.panel1.Controls.Add(this.splitter1);
			this.panel1.Controls.Add(this.treeView);
			this.panel1.Location = new System.Drawing.Point(8, 8);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(672, 320);
			this.panel1.TabIndex = 0;
			// 
			// listView
			// 
			this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this._id,
																					   this._gg,
																					   this._pn,
																					   this._unit,
																					   this._price});
			this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.listView.FullRowSelect = true;
			this.listView.LabelEdit = true;
			this.listView.Location = new System.Drawing.Point(259, 0);
			this.listView.Name = "listView";
			this.listView.Size = new System.Drawing.Size(411, 318);
			this.listView.TabIndex = 2;
			this.listView.View = System.Windows.Forms.View.Details;
			// 
			// _gg
			// 
			this._gg.Text = "规格";
			this._gg.Width = 79;
			// 
			// _pn
			// 
			this._pn.Text = "品名";
			this._pn.Width = 120;
			// 
			// _unit
			// 
			this._unit.Text = "单位";
			this._unit.Width = 47;
			// 
			// _price
			// 
			this._price.Text = "价格";
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(256, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 318);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// treeView
			// 
			this.treeView.Dock = System.Windows.Forms.DockStyle.Left;
			this.treeView.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.treeView.ImageIndex = -1;
			this.treeView.Location = new System.Drawing.Point(0, 0);
			this.treeView.Name = "treeView";
			this.treeView.SelectedImageIndex = -1;
			this.treeView.Size = new System.Drawing.Size(256, 318);
			this.treeView.TabIndex = 0;
			this.treeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterCheck);
			this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(536, 336);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = "已选好";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(608, 336);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 23);
			this.button2.TabIndex = 2;
			this.button2.Text = "取 消";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(8, 336);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(136, 23);
			this.button3.TabIndex = 3;
			this.button3.Text = "管理产品目录";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// _id
			// 
			this._id.Text = "编号";
			this._id.Width = 100;
			// 
			// SelectPC
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(690, 367);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.panel1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SelectPC";
			this.Text = "选择产品";
			this.Load += new System.EventHandler(this.SelectPC_Load);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 取消选择
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button2_Click(object sender, System.EventArgs e) {
			this.Close();
		}

		/// <summary>
		/// 管理产品目录
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e) {
			EditPC epc = new EditPC();
			epc.ShowDialog();
		}

		/// <summary>
		/// 选中节点后将选中的节点移动右窗口中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) {

			string _bh = null;

			TreeNode tn = this.treeView.SelectedNode;
			if(tn!=null){
				_bh = tn.Tag.ToString();
			}

			if(_bh != null){

				for(int i=0;i<ds.Tables[0].Rows.Count;i++){

					DataRow row = ds.Tables[0].Rows[i];
					if(row["treeID"].ToString().Equals(_bh)){
						
						//先检查是否已包含
						bool isAction = true;
						int  _index   = -1;
						for(int j=0;j<this.listView.Items.Count;j++){
							if(this.listView.Items[j].SubItems[0].Text.ToString().Equals(_bh)){
								_index = j;
								j = this.listView.Items.Count;
								isAction = false;
							}
						}

						if(isAction && tn.Checked){
							ListViewItem li = new ListViewItem(new string[]{_bh,row["gg"].ToString(),row["signName"].ToString(),row["unitName"].ToString(),row["price"].ToString()});
							this.listView.Items.Add(li);
						}
						
						if(!tn.Checked && _index > -1){
							this.listView.Items.RemoveAt(_index);
						}
					}
				}			
			}
		}

		/// <summary>
		/// 选好了
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e) {

			//父窗口的列表视图
			DataGrid dg = this.GetParentDG();

			//将原数据复制一份,用于核对数据量、总金额和备注
			DataTable odt = ((DataTable)dg.DataSource).Copy();

			//新数据表
			DataTable ndt = (DataTable)dg.DataSource;
			ndt.Rows.Clear();

			//根据本窗口的设置重新设置数据源
			for(int i=0;i<this.listView.Items.Count;i++){
				ListViewItem lv = this.listView.Items[i];

				DataRow row = ndt.NewRow();
				row[0] = lv.SubItems[2].Text;
				row[1] = lv.SubItems[1].Text;
				
				row[3] = lv.SubItems[3].Text;
				row[4] = lv.SubItems[4].Text;
				row[7] = lv.SubItems[0].Text;

				bool oaction = true;

				//数量从原数据中查找
				for(int j=0;j<odt.Rows.Count;j++){
					if(odt.Rows[j][7].ToString().Equals(lv.SubItems[0].Text)){
						row[2] = odt.Rows[j][2];
						row[5] = odt.Rows[j][5];
						row[6] = odt.Rows[j][6];
						oaction = false;
					}
				}

				if(oaction){
					row[2] = 0;
					row[5] = 0;
				}

				ndt.Rows.Add(row);
			}

			this.Close();
		}

		private void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) {
			//
		}

		/// <summary>
		/// 在树上点击右键时
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ClickInTreeView(object sender,MouseEventArgs e){
			TreeNode tn = this.treeView.GetNodeAt(e.X,e.Y) as TreeNode;	
			this.treeView.SelectedNode = tn;
		}

		private void SelectPC_Load(object sender, System.EventArgs e) {
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -