📄 药价查询.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
{
DataSet ds;
//DataTable dt;
SqlDataAdapter da;
SqlConnection cn = new SqlConnection();
PrintDialog pdlg = new PrintDialog();
internal PrintDocument PDoc = new PrintDocument();
public 药价信息()
{
InitializeComponent();
}
private void 药价查询_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“医院管理系统DataSet.Medicine_Store”中。您可以根据需要移动或移除它。
//this.medicine_StoreTableAdapter.Fill(this.医院管理系统DataSet.Medicine_Store);
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
cn.ConnectionString = "Data Source=sclab76\\SQLEXPRESS;Initial Catalog=医院管理系统;User id=sa;Password=niit#1234";
cn.Open();
SqlCommand cmd = new SqlCommand("Select Medicine_Code,Medicine_Name from Medicine_Store", cn);
SqlDataReader 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)
{
SqlCommand cmd;
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!="")&&(comboBox2.Text!=""))
{
MessageBox.Show("查询条件有冲突,请只输入一个查询条件");
cn.Close();
return;
}
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 button3_Click(object sender, EventArgs e)
{
this.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 button2_Click(object sender, EventArgs e)
{
pdlg.Document = PDoc;
DialogResult result = pdlg.ShowDialog();
if (result == DialogResult.OK)
PDoc.Print();
}
private void button4_Click(object sender, EventArgs e)
{
PrintPreviewDialog ob = new PrintPreviewDialog();
ob.Document = PDoc;
ob.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -