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

📄 form3.cs

📁 某企业ERP,人员基本信息模块
💻 CS
📖 第 1 页 / 共 2 页
字号:
																						this.toolBarButton1,
																						this.toolBarButton2,
																						this.toolBarButton3,
																						this.toolBarButton8,
																						this.toolBarButton10,
																						this.toolBarButton4,
																						this.toolBarButton5,
																						this.toolBarButton6});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(560, 41);
			this.toolBar1.TabIndex = 4;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButton7
			// 
			this.toolBarButton7.Text = "打印";
			// 
			// toolBarButton8
			// 
			this.toolBarButton8.Text = "删除";
			// 
			// toolBarButton10
			// 
			this.toolBarButton10.Text = "刷新";
			// 
			// Form3
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(560, 289);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.toolBar1);
			this.Menu = this.mainMenu1;
			this.Name = "Form3";
			this.Text = "人员基本信息浏览查询";
			this.Load += new System.EventHandler(this.Form3_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Form3_Load(object sender, System.EventArgs e)
		{
			myDataTable = new DataTable();//声明新的实例
			string connection = "server=server;database = sgerp;uid = sa; pwd = sa;";//连接字符串
			mySqlConnection = new SqlConnection(connection);//新建连接
			mySqlDataAdapter = new SqlDataAdapter(test,mySqlConnection);
			mytest = new SqlCommandBuilder(mySqlDataAdapter);
			mySqlDataAdapter.DeleteCommand = mytest.GetDeleteCommand();
			mySqlDataAdapter.InsertCommand = mytest.GetInsertCommand();
			mySqlDataAdapter.UpdateCommand = mytest.GetUpdateCommand();
			dataset1 = new DataSet();
			mySqlDataAdapter.Fill(dataset1,"人员基本信息管理");
			myDataTable = dataset1.Tables["人员基本信息管理"];
			dataGrid1.DataSource = dataset1.Tables["人员基本信息管理"];
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
////////////////////////////修改///////////////////////////////////////////////////////////////
			if ( e.Button == toolBarButton1 )
			{
				Form2 frm2 = new Form2();
				frm2.receive = test;
				int i = dataGrid1.CurrentRowIndex;//交换变量
				frm2.position = i;//用此值来确定具体在新建修改界面中使用哪一行数据
				frm2.remark = 0;//将新建修改标志位置为修改				
				frm2.ShowDialog();
			}
				///保存////////////////////////////////////////////////////////////////////////////////////////
			else if ( e.Button == toolBarButton2 )
			{				
				mySqlDataAdapter.Update(dataset1,"人员基本信息管理");
				dataset1.AcceptChanges();
				dataGrid1.Refresh();
				//mySqlConnection.Close();
			}
				/////////////////取消///////////////////////////////////////////////////////////////////////////////
			else if ( e.Button == toolBarButton3 )
			{
				dataset1.RejectChanges();
				dataGrid1.Refresh();
			}
				///////////////////////////////////返回////////////////////////////////////////////////////////////
			else if ( e.Button == toolBarButton4 )
			{
				this.Close();
			}
				/////////////////////////////////退出////////////////////////////////////////////////////////////////
			else if ( e.Button == toolBarButton5 )
			{
				Application.Exit();
			}
			else if ( e.Button == toolBarButton7 )
			{
				if(MessageBox.Show("确定要导出为EXCEL格式吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question).ToString()=="OK")
				{
					exportData();
				}

			}
				/////////////////////////////////////////////删除行///////////////////////////////////////////
			else if ( e.Button == toolBarButton8 )
			{
				if(MessageBox.Show("确定要删除?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question).ToString()=="OK")
				{
					int i = dataGrid1.CurrentRowIndex; //获取选定行的索引值
					myDataRow = myDataTable.Rows[i];
					myDataRow.Delete();
					dataGrid1.Refresh();
					mySqlDataAdapter.Update(dataset1,"人员基本信息管理");
					dataset1.AcceptChanges();/////重新刷新到
					dataGrid1.DataSource = dataset1.Tables["人员基本信息管理"];//重新绑定
				}		 
			}
				///////////////////////////////////////////////////////////刷新//////////////////////////////////////////////
			else if ( e.Button == toolBarButton10 )
			{
				dataset1.Clear();
				mySqlDataAdapter.Fill(dataset1,"人员基本信息管理");
			}
		}

		private void exportData()
		{
			Object missing=Missing.Value;
			//================保存新文件=================================
			SaveFileDialog saveFileDialog1=new SaveFileDialog();
			saveFileDialog1.FileName="人员基本信息表";
			saveFileDialog1.Filter = "Microsoft Excel|*.xls";
			saveFileDialog1.Title = "保存Excel统计表";
			string fileAdd="";
			if(saveFileDialog1.ShowDialog()==DialogResult.OK)
			{
				Cursor.Current=Cursors.WaitCursor;
				if(saveFileDialog1.FileName!="")
				{
					fileAdd=saveFileDialog1.FileName;
					if(File.Exists(fileAdd))
					{
						try
						{
							File.Delete(fileAdd);
						}
						catch
						{
							MessageBox.Show("您所要覆盖的同名文件处于打开状态,请先关闭该文件再进行操作!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
							Cursor.Current=Cursors.Default;
							return;
						}
						
					}
				}
			}
			else
			{
				return;
			}
			this.Update();
			Excel.ApplicationClass xlApp=new Excel.ApplicationClass();
			if (xlApp==null)
			{
				this.killExcel();
				Cursor.Current=Cursors.Default;
				MessageBox.Show("您没有装有EXCEL!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			xlApp.Visible=false;
			Excel.Workbook wb;
			try
			{
				wb=xlApp.Workbooks.Add("E:\\2006-07-16\\人员基本信息管理主界面\\bin\\Debug\\print\\@人员基本信息表.xls");//path+
			}
			catch
			{
				this.killExcel();
				Cursor.Current=Cursors.Default;
				MessageBox.Show("没有在程序的print文件夹下找到名为\"@人员基本信息表.xls\"的Excel模板!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			//ExcelOut.Application.AltStartupPath="C:\\下料单通用模版.xls";
			
			try
			{
				int j=2;
				int count = dataset1.Tables["人员基本信息管理"].Columns.Count;
				foreach(DataRow myRow in dataset1.Tables["人员基本信息管理"].Rows)
				{
					for(int i=0;i<count;i++)
					{
						xlApp.Cells[j,i+1]=myRow[i];
					}
					j++;
				}
			}
			catch
			{
				this.killExcel();
				Cursor.Current=Cursors.Default;
				MessageBox.Show("保存到Excel失败!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			wb.SaveAs(fileAdd,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);  
			//===========================================================
			this.killExcel();
			MessageBox.Show("保存到Excel成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			Cursor.Current=Cursors.Default;
		}
		//==================清除进程===============
		private void killExcel()
		{
			Process [] localByName = System.Diagnostics.Process.GetProcessesByName("Excel");
			foreach(Process excelapp in localByName)
			{
				if((int)excelapp.MainWindowHandle==0)
				{
					excelapp.Kill();
					excelapp.Close();
				}
			}
		}

	}
}

⌨️ 快捷键说明

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