?? endowmentinfoform.cs
字號:
this.MinimizeBox = false;
this.Name = "EndowmentInfoForm";
this.Text = "養老保險記錄";
this.Load += new System.EventHandler(this.EndowmentInfoForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void EndowmentInfoForm_Load(object sender, System.EventArgs e)
{
DBDataGrid();
switch(Form1.f1.power)
{
case "系統管理員":
{
this.toolBarButton5.Enabled = true;
this.toolBarButton6.Enabled = true;
this.toolBarButton7.Enabled = true;
break;
}
case "管理員":
{
this.toolBarButton5.Enabled = true;
this.toolBarButton6.Enabled = true;
this.toolBarButton7.Enabled = true;
break;
}
case "一般用戶":
{
this.toolBarButton5.Enabled = false;
this.toolBarButton6.Enabled = false;
this.toolBarButton7.Enabled = false;
break;
}
default :
{
this.toolBarButton5.Enabled = false;
this.toolBarButton6.Enabled = false;
this.toolBarButton7.Enabled = false;
break;
}
}
}
#region DataGrid數據綁定
public void DBDataGrid()
{
string s = "select EndowmentInfo.eeeId,EndowmentInfo.endId as 員工編號,EmployInfo.eName as 員工姓名,EndowmentInfo.enId as 養老保險號,EndowmentInfo.enDate as 發放時間,";
s += "EndowmentInfo.enMonney as 養老保險金 from EndowmentInfo,EmployInfo where EndowmentInfo.endId=EmployInfo.endId order by EndowmentInfo.endId asc";
Base bb= new Base();
this.dt = bb.ExeSQLdt(s);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,this.dataGrid1);
}
#endregion
#region 工具欄事件
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch(e.Button.ToolTipText)
{
case "首記錄":
{
if(cm.Position != 0)
{
this.dataGrid1.UnSelect(cm.Position);
cm.Position = 0;
this.dataGrid1.Select(cm.Position);
this.dataGrid1.CurrentRowIndex = cm.Position;
}
break;
}
case "上記錄":
{
if(cm.Position > 0)
{
this.dataGrid1.UnSelect(cm.Position );
cm.Position -= 1;
this.dataGrid1.Select(cm.Position);
this.dataGrid1.CurrentRowIndex = cm.Position;
}
break;
}
case "下記錄":
{
if(cm.Position < cm.Count - 1)
{
this.dataGrid1.UnSelect(cm.Position );
cm.Position += 1;
this.dataGrid1.Select(cm.Position);
this.dataGrid1.CurrentRowIndex = cm.Position;
}
break;
}
case "尾記錄":
{
if(cm.Position != cm.Count - 1)
{
this.dataGrid1.UnSelect(cm.Position);
cm.Position = cm.Count - 1;
this.dataGrid1.Select(cm.Count - 1);
this.dataGrid1.CurrentRowIndex = cm.Position;
}
break;
}
case "添加":
{
this.insert(sender,e);
break;
}
case "修改":
{
this.updata(sender,e);
break;
}
case "刪除":
{
this.delete(sender,e);
break;
}
case "退出":
{
this.close(sender,e);
break;
}
case "查找":
{
this.search(sender,e);
break;
}
case "打印":
{
break;
}
}
}
#endregion
#region 工具欄具體事件方法
private void insert(object sender, System.EventArgs e)
{
EndowmentInfoForm.ewID = "insert";
this.Enabled = false;
if(mainForm.mf.CheckForm("EndowmentRecord") == true)
{
return ;
}
else
{
EndowmentRecord ewf = new EndowmentRecord();
ewf.MdiParent = mainForm.mf;
ewf.Show();
}
}
private void updata(object sender, System.EventArgs e)
{
EndowmentInfoForm.ewID = "update";
this.Enabled = false;
this.sb.Remove(0,this.sb.ToString().Length);
for(int i = 0 ; i < this.dt.Columns.Count ; i++)
{
this.sb.Append(this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,i].ToString() + "#");
}
if(mainForm.mf.CheckForm("EndowmentRecord") == true)
{
return ;
}
else
{
EndowmentRecord ewf = new EndowmentRecord();
ewf.MdiParent = mainForm.mf;
ewf.Show();
}
}
private void delete(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show("是否刪除此記錄?","提示!",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(result == DialogResult.Yes)
{
string str = "update EndowmentInfo set enDate=null,enMonney=null where eeeId=" + int.Parse(this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString());
Base bb = new Base();
bb.ExeSQL(str);
this.DBDataGrid();
MessageBox.Show("刪除成功!");
}
if(result == DialogResult.No)
{
return;
}
}
private void close(object sender, System.EventArgs e)
{
this.Close();
}
private void search(object sender, System.EventArgs e)
{
this.groupBox1.Visible = true;
this.dataGrid1.Dock = DockStyle.None;
this.dataGrid1.Location = new Point(0,88);
this.dataGrid1.Height = 368;
}
#endregion
#region 右鍵快捷菜單事件
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
if(this.contextMenu1.SourceControl == this.dataGrid1)
{
this.contextMenu1.MenuItems.Clear();
this.contextMenu1.MenuItems.Add("添加記錄",new EventHandler(this.insert));
this.contextMenu1.MenuItems.Add("修改記錄",new EventHandler(this.updata));
this.contextMenu1.MenuItems.Add("刪除記錄",new EventHandler(this.delete));
this.contextMenu1.MenuItems.Add("-");
this.contextMenu1.MenuItems.Add("查找",new EventHandler(this.search));
this.contextMenu1.MenuItems.Add("-");
this.contextMenu1.MenuItems.Add("打印");
this.contextMenu1.MenuItems[6].MenuItems.Add("頁面設置",new EventHandler(this.menuItem1_Click));
this.contextMenu1.MenuItems[6].MenuItems.Add("打印預覽",new EventHandler(this.menuItem2_Click));
this.contextMenu1.MenuItems[6].MenuItems.Add("打印",new EventHandler(this.menuItem3_Click));
this.contextMenu1.MenuItems.Add("-");
this.contextMenu1.MenuItems.Add("退出",new EventHandler(this.close));
}
}
#endregion
//取消查找
private void button2_Click(object sender, System.EventArgs e)
{
this.groupBox1.Visible = false;
this.dataGrid1.Dock = DockStyle.Fill;
this.DBDataGrid();
}
//查找記錄
private void button1_Click(object sender, System.EventArgs e)
{
string s = "select EndowmentInfo.eeeId,EndowmentInfo.endId as 員工編號,EmployInfo.eName as 員工姓名,EndowmentInfo.enId as 養老保險號,EndowmentInfo.enDate as 發放時間,";
s += "EndowmentInfo.enMonney as 養老保險金 from EndowmentInfo,EmployInfo where EndowmentInfo.endId=EmployInfo.endId";
if(this.ID_txt.Text.Trim() != "")
{
s += " and EndowmentInfo.endId=" + int.Parse(this.ID_txt.Text.Trim());
Base bb= new Base();
this.dt = bb.ExeSQLdt(s);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,this.dataGrid1);
}
if(this.name_txt.Text.Trim() != "")
{
s += " and EmployInfo.eName like '%[" + this.name_txt.Text.Trim() + "]%'";
Base bb= new Base();
this.dt = bb.ExeSQLdt(s);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,this.dataGrid1);
}
if(this.Date_txt.Text != "")
{
if(Regexlib.IsValidData(this.Date_txt.Text.Trim()))
{
s += " and EndowmentInfo.enDate='" + this.Date_txt.Text.Trim() + "'";
Base b1 = new Base();
this.dt = b1.ExeSQLdt(s);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,this.dataGrid1);
}
else
{
MessageBox.Show("發放時間格式錯誤,請輸入(yyyy-mm-dd)");
}
}
}
private void ID_txt_TextChanged(object sender, System.EventArgs e)
{
if(this.ID_txt.Text.Trim() != "")
{
if(!Regexlib.IsValidNumber(this.ID_txt.Text.Trim()))
{
MessageBox.Show("員工編號格式錯誤,請輸入正整數!");
}
}
}
//頁面設置
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
this.pageSetupDialog1.Document = this.printDocument1;
}
//打印預覽
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.printPreviewDialog1.ShowDialog();
}
//打印
private void menuItem3_Click(object sender, System.EventArgs e)
{
if(this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch(Exception ec)
{
MessageBox.Show(ec.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument1.PrintController.OnEndPrint(printDocument1,new PrintEventArgs());
}
}
}
#region 打印時,畫相應的數據到打印控件中
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;//創建一個畫布
float leftMargin = e.MarginBounds.Left;
float left = e.MarginBounds.Left;//X坐標
float top = e.MarginBounds.Top;//Y坐標
Font TitltFont = new Font("楷體_GB2312",12);//大標題字體
Font SubTileFont = new Font("楷體_GB2312",8);//小標題字體
Font font = this.dataGrid1.Font;//數據字體為DataGrid的默認字體
SolidBrush brush = new SolidBrush(Color.Black);//畫刷
g.DrawString("員工養老保險金發放記錄表",TitltFont,brush,e.MarginBounds.Width/2,top);//打印主標題
g.DrawString("打印日期: " + DateTime.Now.ToShortDateString(),SubTileFont,brush,e.MarginBounds.Width/4*3,top + font.GetHeight(g)*2);//打印小標題(打印出打印的日期)
DataGridTableStyle Grid = new DataGridTableStyle();//定義一個DataGridD的表樣式的對象
this.dataGrid1.TableStyles.Add(Grid);
Grid.GridColumnStyles[0].Width = 0;
StringBuilder sbHeadText = new StringBuilder();
foreach(DataGridColumnStyle myDataGridCol in Grid.GridColumnStyles)//獲取DataGrid的列名存進StringBuilder中
{
sbHeadText.Append(myDataGridCol.HeaderText + "#");
}
string [] headText = sbHeadText.ToString().Split('#');//把StringBuilder分成字符串
for(int i = 1 ; i < headText.Length ; i ++)//在畫出列名
{
g.DrawString(headText[i],font,brush,left,top + font.GetHeight(g)*4);
left += 140;
}
float Top = top + font.GetHeight(g)*5;//畫數據時的Y坐標
for(int m = 0 ; m < this.dt.Rows.Count ; m ++)
{
leftMargin = e.MarginBounds.Left;
for(int n = 1 ; n < this.dt.Columns.Count ;n ++)
{
g.DrawString(this.dt.Rows[m][n].ToString(),font,brush,leftMargin,Top);
leftMargin += 140;
}
Top += font.GetHeight(g)*2;
}
if(Top > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
#endregion
private void ID_txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
this.button1_Click(sender,e);
}
}
private void name_txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
this.button1_Click(sender,e);
}
}
private void Date_txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
this.button1_Click(sender,e);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -