📄 工资管理.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Printing;
namespace WindowsApplication1
{
public partial class 工资管理 : Form
{
SqlCommand cmd;
DataSet ds;
SqlDataReader dr;
SqlDataAdapter da;
SqlConnection cn = new SqlConnection();
PrintDialog pdlg = new PrintDialog();
internal PrintDocument PDoc = new PrintDocument();
internal PrintDocument PDoc2 = new PrintDocument();
public 工资管理()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
cn.ConnectionString = "Data Source=sclab76\\SQLEXPRESS;Initial Catalog=医院管理系统;User id=sa;Password=niit#1234";
cn.Open();
SqlTransaction tran = tran = cn.BeginTransaction(); ;
try
{
da = new SqlDataAdapter();
da.SelectCommand = (new SqlCommand("select * from Salary", cn, tran));
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.Update(ds, "Salary");
tran.Commit();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
tran.Rollback();
}
finally
{
cn.Close();
}
}
private void Form6_Load(object sender, EventArgs e)
{
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
PDoc2.PrintPage += new PrintPageEventHandler(PDoc2_PrintPage);
cn.ConnectionString = "Data Source=sclab76\\SQLEXPRESS;Initial Catalog=医院管理系统;User id=sa;Password=niit#1234";
cn.Open();
ds = new DataSet();
da = new SqlDataAdapter();
cmd = new SqlCommand("Select * from Salary", cn);
da.SelectCommand = cmd;
da.Fill(ds, "Salary");
dataGridView2.DataSource = ds;
dataGridView2.DataMember = "Salary";
cmd = new SqlCommand("Select Number,Name from Salary", cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["Number"].ToString());
comboBox2.Items.Add(dr["Name"].ToString());
}
cn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
cn.ConnectionString = "Data Source=sclab76\\SQLEXPRESS;Initial Catalog=医院管理系统;User id=sa;Password=niit#1234";
cn.Open();
ds = new DataSet();
da = new SqlDataAdapter();
if ((comboBox1.Text == "") && (comboBox2.Text == ""))
{
cmd = new SqlCommand("Select * from Salary", cn);
}
else if((comboBox1.Text != "")&&(comboBox2.Text != ""))
{
MessageBox.Show("只能有一个查询条件,请重新输入");
cn.Close();
return;
}
else
{
if (comboBox1.Text != "")
{
cmd = new SqlCommand("Select * from Salary where Number=@code", cn);
cmd.Parameters.Add(new SqlParameter("@code", comboBox1.Text));
}
else
{
cmd = new SqlCommand("Select * from Salary where Name=@name", cn);
cmd.Parameters.Add(new SqlParameter("@name", comboBox2.Text));
}
}
da.SelectCommand = cmd;
da.Fill(ds, "Salary");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Salary";
cn.Close();
}
//--------------------------------------------------------------------------------------------------------------
private void PDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 20;
int y = 20;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
x = 20;
y += 50;
if (y >= e.PageBounds.Height - 80)
{
e.HasMorePages = true;
return;
}
e.HasMorePages = false;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[j].Value.ToString(), new Font("Arial", 20, FontStyle.Bold), Brushes.Black, x, y);
x += 200;
if (x >= e.PageBounds.Right - 20)
{
x = 20;
}
}
}
}
private void button7_Click(object sender, EventArgs e)
{
PrintPreviewDialog ob = new PrintPreviewDialog();
ob.Document = PDoc;
ob.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
pdlg.Document = PDoc;
DialogResult result = pdlg.ShowDialog();
if (result == DialogResult.OK)
PDoc.Print();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
private void PDoc2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 20;
int y = 20;
e.Graphics.DrawString("经办人: "+textBox1.Text, new Font("Arial", 20, FontStyle.Bold), Brushes.Black, x, y);
y += 50;
e.Graphics.DrawString("日期: "+dateTimePicker1.Text, new Font("Arial", 20, FontStyle.Bold), Brushes.Black, x, y);
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
x = 20;
y += 50;
if (y >= e.PageBounds.Height - 80)
{
e.HasMorePages = true;
return;
}
e.HasMorePages = false;
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
e.Graphics.DrawString(dataGridView2.Rows[i].Cells[j].Value.ToString(), new Font("Arial", 20, FontStyle.Bold), Brushes.Black, x, y);
x += 200;
if (x >= e.PageBounds.Right - 20)
{
x = 20;
}
}
}
}
private void button6_Click_1(object sender, EventArgs e)
{
PrintPreviewDialog ob = new PrintPreviewDialog();
ob.Document = PDoc2;
ob.ShowDialog();
}
private void button4_Click_1(object sender, EventArgs e)
{
pdlg.Document = PDoc2;
DialogResult result = pdlg.ShowDialog();
if (result == DialogResult.OK)
PDoc2.Print();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -