?? form3.cs
字號:
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 + -