📄 societyinfoform.cs
字号:
this.Name = "SocietyInfoForm";
this.Text = "社会关系";
this.EnabledChanged += new System.EventHandler(this.SocietyInfoForm_EnabledChanged);
this.Load += new System.EventHandler(this.SocietyInfoForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void SocietyInfoForm_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 str = "select SocietyInfo.sssId,SocietyInfo.seId as 员工编号,EmployInfo.eName as 员工姓名,SocietyInfo.sName as 姓名,SocietyInfo.sLaction as 关系,";
str += "SocietyInfo.sUinte as 工作单位和职务,SocietyInfo.sRemarks as 备注 from SocietyInfo,EmployInfo where SocietyInfo.seId=EmployInfo.seId order by SocietyInfo.seId asc";
Base bb = new Base();
this.dt = bb.ExeSQLdt(str);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,6,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;
}
case "列选择":
{
this.Colselect5(sender,e);
break;
}
}
}
#endregion
#region 工具栏的具体事件方法
//添加
private void insert(object sender, System.EventArgs e)
{
SocietyInfoForm.sID = "insert";
if(mainForm.mf.CheckForm("SocietyInfoRecord") == true)
{
return ;
}
else
{
SocietyInfoRecord sifr = new SocietyInfoRecord();
sifr.MdiParent = mainForm.mf;
sifr.Show();
}
}
//修改
private void updata(object sender, System.EventArgs e)
{
SocietyInfoForm.sID = "update";
this.sb.Remove(0,this.sb.ToString().Length);
for(int i = 0 ; i < this.dt.Columns.Count ; i ++)
{
sb.Append(this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,i].ToString() + "#");
}
if(mainForm.mf.CheckForm("SocietyInfoRecord") == true)
{
return ;
}
else
{
SocietyInfoRecord sifr = new SocietyInfoRecord();
sifr.MdiParent = mainForm.mf;
sifr.Show();
}
}
//删除
private void delete(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show("是否删除此记录?","提示!",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(result == DialogResult.Yes)
{
string str = "update SocietyInfo set sName=null,sLaction=null,sUinte=null,sRemarks=null where sssId=" + 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 Colselect5(object sender, System.EventArgs e)
{
if(mainForm.mf.CheckForm("ColselectForm5") == true)
{
return ;
}
else
{
ColselectForm5 col5 = new ColselectForm5();
col5.MdiParent = mainForm.mf;
col5.Show();
}
}
private void search(object sender, System.EventArgs e)
{
this.groupBox1.Visible = true;
this.dataGrid1.Dock = DockStyle.None;
this.dataGrid1.Location = new Point(0,96);
this.dataGrid1.Height = 352;
}
//退出
private void close(object sender, System.EventArgs e)
{
this.Close();
}
#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.Colselect5));
this.contextMenu1.MenuItems.Add("-");
this.contextMenu1.MenuItems.Add("退出",new EventHandler(this.close));
}
}
#endregion
private void SocietyInfoForm_EnabledChanged(object sender, System.EventArgs e)
{
this.DBDataGrid();
}
//取消
private void button2_Click(object sender, System.EventArgs e)
{
this.groupBox1.Visible = false;
this.dataGrid1.Dock = DockStyle.Fill;
this.DBDataGrid();
}
private void ID_txt_TextChanged(object sender, System.EventArgs e)
{
if(this.ID_txt.Text != "")
{
if(!Regexlib.IsValidNumber(this.ID_txt.Text.Trim()))
{
MessageBox.Show("员工编号格式错误,请输入正整数!");
}
}
}
//查找记录
private void button1_Click(object sender, System.EventArgs e)
{
string str = "select SocietyInfo.sssId,SocietyInfo.seId as 员工编号,EmployInfo.eName as 员工姓名,SocietyInfo.sName as 姓名,SocietyInfo.sLaction as 关系,";
str += "SocietyInfo.sUinte as 工作单位和职务,SocietyInfo.sRemarks as 备注 from SocietyInfo,EmployInfo where SocietyInfo.seId=EmployInfo.seId";
if(this.ID_txt.Text.Trim() != "")
{
str += " and SocietyInfo.seId=" + int.Parse(this.ID_txt.Text.Trim());
Base bb = new Base();
this.dt = bb.ExeSQLdt(str);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,6,this.dataGrid1);
}
if(this.name_txt.Text.Trim() != "")
{
str += " and SocietyInfo.sName='" + this.name_txt.Text.Trim() + "'";
Base bb = new Base();
this.dt = bb.ExeSQLdt(str);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,6,this.dataGrid1);
}
if(this.comboBox1.Text != "")
{
str += " and SocietyInfo.sLaction='" + this.comboBox1.SelectedItem.ToString() + "'";
Base bb = new Base();
this.dt = bb.ExeSQLdt(str);
this.dataGrid1.DataSource = this.dt.DefaultView;
this.cm = (CurrencyManager)BindingContext[this.dt];
File.DataGridStyle(this.dt,6,this.dataGrid1);
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
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 error)
{
MessageBox.Show(error.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument1.PrintController.OnEndPrint(printDocument1,new PrintEventArgs());
}
}
}
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 += 100;
}
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 += 100;
}
Top += font.GetHeight(g)*2;
}
if(Top > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
private void printPreviewDialog1_Closed(object sender, System.EventArgs e)
{
this.DBDataGrid();
}
#region 回车事件
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 comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
this.button1_Click(sender,e);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -