Form1.cs
上传用户:lfy307
上传日期:2022-07-17
资源大小:79k
文件大小:3k
- 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;
- namespace xiaopiaodayin
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string strconn = "Data Source=(local);Initial Catalog=thedatabase;User ID=sa";
- DataSet ds;
- SqlDataAdapter da;
- private void button1_Click(object sender, EventArgs e)
- {
- this.printDocument1.Print();
- //this.printPreviewDialog1.Document = this.printDocument1;
- //printPreviewDialog1.Show();
- }
-
- private void xiaopiao_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
- {
- e.Graphics.DrawString("爱因森08软件技术4班", new Font("宋体", 12, FontStyle.Regular), Brushes.Black, 25, 10);
- e.Graphics.DrawString("超市销售小票", new Font("宋体", 12, FontStyle.Regular), Brushes.Black, 55, 30);
- e.Graphics.DrawLine(new Pen(Brushes.Black,2), 25, 55, 200, 55);
- int i=0;
- int sum = 0;
- foreach(DataRow dr in ds.Tables["Goods"].Rows)
- {
- e.Graphics.DrawString(dr[1].ToString(), new Font("宋体", 10, FontStyle.Regular), Brushes.Black, 25, 70+i*20);
- e.Graphics.DrawString(dr[2].ToString(), new Font("宋体", 10, FontStyle.Regular), Brushes.Black, 150, 70+i*20);
- int a=Convert.ToInt32(dr[2]);
- sum = sum + a;
- i++;
- }
- string sumstr = Convert.ToString(sum)+"元";
- e.Graphics.DrawLine(new Pen(Brushes.Black, 2), 25, 70 + i * 20, 200, 70 + i * 20);
- e.Graphics.DrawString("总价:", new Font("宋体", 10, FontStyle.Regular), Brushes.Black, 25, 80 + i * 20);
- e.Graphics.DrawString(sumstr, new Font("宋体", 10, FontStyle.Regular), Brushes.Black, 140, 80 + i * 20);
-
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- SqlConnection conn = new SqlConnection(strconn);
- conn.Open();
- string sql = "select * from Goods";
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sql;
- da = new SqlDataAdapter(cmd);
- ds = new DataSet();
- da.Fill(ds, "Goods");
- conn.Close();
- this.dataGridView1.DataSource=ds.Tables["Goods"];
- }
- private void button2_Click(object sender, EventArgs e)
- {
- SqlCommandBuilder cb = new SqlCommandBuilder(da);
- da.Update(ds, "Goods");
- }
- }
- }