📄 库存查询.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 Form5_Load(object sender, EventArgs e)
{
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
cn.ConnectionString = "Data Source=sclab76\\SQLEXPRESS;Initial Catalog=医院管理系统;User id=sa;Password=niit#1234";
cn.Open();
cmd = new SqlCommand("Select Medicine_Code,Medicine_Name from Medicine_Store", cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["Medicine_Code"].ToString());
comboBox2.Items.Add(dr["Medicine_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 Medicine_Store", cn);
}
else
{
if (comboBox1.Text!= "")
{
cmd = new SqlCommand("Select * from Medicine_Store where Medicine_Code=@code", cn);
cmd.Parameters.Add(new SqlParameter("@code", comboBox1.Text));
}
else
{
cmd = new SqlCommand("Select * from Medicine_Store where Medicine_Name=@name", cn);
cmd.Parameters.Add(new SqlParameter("@name", comboBox2.Text));
}
}
da.SelectCommand = cmd;
da.Fill(ds, "MedicineStore");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "MedicineStore";
dataGridView1.Columns[0].HeaderText = "MedicineCode";
dataGridView1.Columns[1].HeaderText = "MedicineName";
dataGridView1.Columns[2].HeaderText = "Price";
dataGridView1.Columns[3].HeaderText = "Amount";
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 button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click_1(object sender, EventArgs e)
{
PrintPreviewDialog ob = new PrintPreviewDialog();
ob.Document = PDoc;
ob.ShowDialog();
}
private void button2_Click_1(object sender, EventArgs e)
{
pdlg.Document = PDoc;
DialogResult result = pdlg.ShowDialog();
if (result == DialogResult.OK)
PDoc.Print();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -