?? staffquery.cs
字號:
this.totalNumber.Location = new System.Drawing.Point(352, 312);
this.totalNumber.Name = "totalNumber";
this.totalNumber.ReadOnly = true;
this.totalNumber.Size = new System.Drawing.Size(152, 21);
this.totalNumber.TabIndex = 3;
this.totalNumber.Text = "";
//
// groupBox2
//
this.groupBox2.BackColor = System.Drawing.Color.AliceBlue;
this.groupBox2.Controls.Add(this.Excel_button8);
this.groupBox2.Controls.Add(this.totalNumber);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(8, 72);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(688, 352);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
//
// Excel_button8
//
this.Excel_button8.Location = new System.Drawing.Point(512, 312);
this.Excel_button8.Name = "Excel_button8";
this.Excel_button8.Size = new System.Drawing.Size(104, 23);
this.Excel_button8.TabIndex = 15;
this.Excel_button8.Text = "導(dǎo)出Excel";
this.Excel_button8.Click += new System.EventHandler(this.Excel_button8_Click);
//
// StaffQuery
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.AliceBlue;
this.ClientSize = new System.Drawing.Size(704, 437);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.groupBox2);
this.Location = new System.Drawing.Point(140, 0);
this.Name = "StaffQuery";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "員工信息查詢";
this.Load += new System.EventHandler(this.StaffQuery_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void StaffQuery_Load(object sender, System.EventArgs e)
{
this.loadStaff();
this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
}
public void loadStaff()
{
string sql = "select 編號,姓名,Email,銀行卡號 from employee where 狀態(tài)=0 order by 編號 asc";
showData(sql);
}
//執(zhí)行sql語句把查詢出的條件
public void showData(string sql)
{
try
{
if(dbCon==null) dbCon = MainForm.getConnection();
dbAdapter = new OleDbDataAdapter(sql , dbCon);
dbAdapter.TableMappings.Add("Table","employee");
ds=new DataSet("employee");
ds.Clear();
dbAdapter.Fill(ds);
this.dataGrid1.SetDataBinding(ds,"employee");
this.totalNumber.Text = ds.Tables["employee"].Rows.Count.ToString();
}
catch(Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
//單擊查詢員工信息按鈕后的事件處理
private void button1_Click(object sender, System.EventArgs e)
{
string sql = "select 編號,姓名,Email,銀行卡號 from employee where 狀態(tài)=0 ";
//string sql = "select e.autoid,e.name,e.email,e.sex,e.workdate,e.personid,e.homephone,e.officephone,e.mobile,e.address,e.workstatus,e.residence,e.buziness,e.bargainstart,e.bargainend,d.depname,e.mess from employee e,t_dep d where e.depcode=d.depcode and e.status=0 ";
query(sql);
this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
}
//檢查輸入的查詢條件中的用戶銀行卡號是否合法
private string number_Input_Check(string caption,string text)
{
string error="";
text=text.Trim();
if(text=="" )
{
error=caption+"不能為空!!\n";
return(error);
}
int i=0;
for(i=0;i<text.Length;i++)
{
if(text[i]>'9' || text[i]<'0')
break;
}
if(i<text.Length)
{
error=caption+"輸入格式不正確,請修正!!!\n";
return(error);
}
return(error);
}
//根據(jù)條件從數(shù)據(jù)庫中檢索出 符合條件的員工信息
private void query(string sql)
{
try
{
string error="";
if(this.idCheck.Checked)//按編號查詢
{
error+=number_Input_Check("編號",this.empid.Text);
if(!error.Trim().Equals(""))
{
MessageBox.Show(error);
return;
}
long autoid = long.Parse(this.empid.Text);
sql+=" and 編號="+autoid;
}
if(this.nameCheck.Checked)//按員工姓名查詢
{
if(this.empname.Text.Trim().Equals(""))
{
MessageBox.Show("姓名不能為空!!!\n");
return;
}
sql+=" and 姓名='"+this.empname.Text+"' ";
}
if(this.emailcheck.Checked)//按員工Email查詢
{
error+=email_Input_Check();
if(!error.Trim().Equals(""))
{
MessageBox.Show(error);
return;
}
sql+=" and Email='"+this.email.Text+"' ";
}
if(this.bankcardcheck.Checked)//按員工銀行卡帳號查詢
{
error+=number_Input_Check("銀行卡號",this.bankcard.Text);
if(!error.Trim().Equals(""))
{
MessageBox.Show(error);
return;
}
sql+=" and 銀行卡號='"+this.bankcard.Text+"'";
}
sql+=" order by 編號 asc";
this.showData(sql);
}
catch(Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
if(ds==null || ds.Tables["employee"].Rows.Count==0)
{
this.menuItem1.Visible = false;
this.menuItem2.Visible = false;
this.menuItem4.Visible = false;
}
else
{
this.menuItem1.Visible = true;
this.menuItem2.Visible = true;
this.menuItem4.Visible = false;
}
}
private void contextMenu1_Popup1(object sender, System.EventArgs e)
{
if(ds==null || ds.Tables["employee"].Rows.Count==0)
{
this.menuItem1.Visible = false;
this.menuItem2.Visible = false;
this.menuItem4.Visible = false;
}
else
{
this.menuItem1.Visible = false;
this.menuItem2.Visible = false;
this.menuItem4.Visible = true;
}
}
/// <summary>
/// 單擊ContextMenu中的 刪除該條員工信息菜單項 將
/// 把該條員工信息的狀態(tài)置成刪除狀態(tài)1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem2_Click(object sender, System.EventArgs e)
{
long empid = long.Parse(this.dataGrid1[this.dataGrid1.CurrentRowIndex,0].ToString());
//取得用戶選擇的員工信息的編號
try
{
if(dbCon==null) dbCon = MainForm.getConnection();
OleDbCommand cmd = dbCon.CreateCommand();
cmd.CommandText = "update employee set 狀態(tài)=1 where 狀態(tài)=0 and 編號="+empid;
//該條員工信息 置刪除狀態(tài)
int result = cmd.ExecuteNonQuery();
if(result==1)
{
string sql = "select 編號,姓名,Email,銀行卡號 from employee where 狀態(tài)=0 ";
query(sql);//重新加載員工信息
}
cmd.Dispose();
}
catch(Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
/// <summary>
/// 單擊ContextMenu中的 修改該條員工信息菜單項 將打開修改員工信息窗口
/// 修改完成后 本窗口顯示的員工信息將被刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem1_Click(object sender, System.EventArgs e)
{
StaffModify em = new StaffModify(this.dataGrid1);
em.Owner = this;
em.ShowDialog();
string sql = "select 編號,姓名,Email,銀行卡號 from employee where 狀態(tài)=0 ";
query(sql);
}
/// <summary>
/// 單擊ContextMenu中的 恢復(fù)該條員工信息菜單項 將打開恢復(fù)員工信息窗口
/// 將把數(shù)據(jù)庫中該條員工信息狀態(tài)改成有銷狀態(tài) 1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem4_Click(object sender, System.EventArgs e)
{
employee.RenewStaff rwk= new employee.RenewStaff(this.dataGrid1,0);
rwk.Owner = this;
rwk.ShowDialog();
}
/// <summary>
/// 單擊 查詢已刪除信息 按鈕時 從書庫取出所有狀態(tài)為 已刪除狀態(tài)1的員工信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
string sql = "select 編號,姓名,Email,銀行卡號 from employee where 狀態(tài)=1 order by 編號 asc";
this.showData(sql);
this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup1);
}
//檢查輸入的查詢條件的Email地址是否合法
private string email_Input_Check()
{
string error="";
if(this.email.Text=="" || this.email.Text.Trim().Equals(""))
{
error="Email不能為空!!!\n ";
return error;
}
if((this.email.Text.IndexOf("@")==-1) || (this.email.Text.IndexOf(".")==-1))
{
error+="Email格式不正確!!!\n ";
return error;
}
return error;
}
//單擊導(dǎo)出Excel按鈕 將把dataGrid1中的數(shù)據(jù)導(dǎo)出到Excel
private void Excel_button8_Click(object sender, System.EventArgs e)
{
System.Data.DataSet ds = (System.Data.DataSet)this.dataGrid1.DataSource;
if(ds==null|| ds.Tables.Count<=0)
{
MessageBox.Show("您沒查詢,不能導(dǎo)出Excel!!!\n 請進(jìn)行查詢!!");
return;
}
Excel.Application ExcelObj = null;
ExcelObj = new Excel.Application();
ExcelObj.Visible =false;
SaveFileDialog sf = new SaveFileDialog();
HrSalary.util.GridUtil gridutil=new HrSalary.util.GridUtil();
gridutil.GridXls(sf,this.dataGrid1,ExcelObj);
ExcelObj.Quit();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -