接下来我将会写一篇关于简单的书店销售结算系统代码的大概解析
1.以下代码是大概的运行逻辑,就是一个简单的结算页面以及打印页面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BookShopTuto
{
public partial class FrmBilling : Form
{
public FrmBilling()
{
InitializeComponent();
populate();
}
string connectionString = "server=.;database=BookDBase;uid=sa;pwd=123456";
private void populate()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "select *from BookInfo";
SqlDataAdapter sda = new SqlDataAdapter(query, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
var ds = new DataSet();
sda.Fill(ds);
UGW.DataSource = ds.Tables[0];
connection.Close();
//加入库存的图书数据库
}
}
private void FrmBilling_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 1000;//时间间隔为1000毫秒
}
private void timer1_Tick(object sender, EventArgs e)
{
//this.toolStripStatusLabel1.Text =
// "当前时间" + System.DateTime.Now.ToString();
}
private void label16_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要退出吗?",
"温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
Application.Exit();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text =
"当前时间" + System.DateTime.Now.ToString();
}
private void UGW_CellContentClick(object sender, DataGridViewCellEventArgs e)
//显示书名 价格 数量
{
txttitle.Text = UGW.SelectedRows[0].Cells[2].Value.ToString();
//显示书名
//BAuTB.Text = BookDGV.SelectedRows[0].Cells[2].Value.ToString();
///BCatCB.Text = BookDGV.SelectedRows[0].Cells[3].Value.ToString();
txtPrice.Text = UGW.SelectedRows[0].Cells[8].Value.ToString();
//显示书籍价格
txtNum.Text = "";
//txtNum.Text = UGW.SelectedRows[0].Cells[7].Value.ToString();
//显示书籍数量(注释掉可以让用户更方便地选择书籍数量)
if (txttitle.Text =="")
{
key = 0;
stock = 0;
}
else
{
key = Convert.ToInt32(UGW.SelectedRows[0].Cells[0].Value.ToString());
stock = Convert.ToInt32(UGW.SelectedRows[0].Cells[7].Value.ToString());
}
}
private void Reset()//重置
{
txttitle.Text = "";
txtPrice.Text = "";
txtNum.Text = "";
}
//重置
int n = 0, GrdTotal = 0;
private void btnSave_Click(object sender, EventArgs e)//加入订单
{
// int n = 0;
//if (txtNum.Text == "" || Convert.ToInt32(txtNum.Text) > stock)
//{
// MessageBox.Show("库存不足!!!");//库存不足时会显示
//}
//else
//{
// int total = Convert.ToInt32(txtNum.Text) * Convert.ToInt32(txtPrice.Text);
// DataGridViewRow newRow = new DataGridViewRow();
// newRow.CreateCells(dataGridView1);
// newRow.Cells[0].Value = n + 1;
// newRow.Cells[1].Value = txttitle.Text;//书名
// newRow.Cells[3].Value = txtNum.Text;//数量
// newRow.Cells[2].Value = txtPrice.Text;//价格
// newRow.Cells[4].Value = total;
// dataGridView1.Rows.Add(newRow);
// n++;
// GrdTotal = GrdTotal + total;
// label1.Text = "¥" + GrdTotal;
// MessageBox.Show("书籍加入成功");
//}以下为完善源码
//-----------------------------------------------------------//
if (txtNum.Text == "" || Convert.ToInt32(txtNum.Text) > stock)
{
MessageBox.Show("库存不足!!!");
}
else
{
bool bookExists = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[1].Value != null && row.Cells[1].Value.ToString() == txttitle.Text)
{
int total = Convert.ToInt32(txtNum.Text) * Convert.ToInt32(txtPrice.Text);
row.Cells[3].Value = Convert.ToInt32(row.Cells[3].Value) + Convert.ToInt32(txtNum.Text); // 将数量相加
row.Cells[4].Value = Convert.ToInt32(row.Cells[4].Value) + total; // 更新总价
GrdTotal = GrdTotal + total;
label1.Text = "¥" + GrdTotal;
MessageBox.Show("书籍加入成功");
bookExists = true;
break;
}
}
if (!bookExists)
{
int total = Convert.ToInt32(txtNum.Text) * Convert.ToInt32(txtPrice.Text);
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(dataGridView1);
newRow.Cells[0].Value = n + 1;
newRow.Cells[1].Value = txttitle.Text;
newRow.Cells[3].Value = txtNum.Text;
newRow.Cells[2].Value = txtPrice.Text;
newRow.Cells[4].Value = total;
dataGridView1.Rows.Add(newRow);
n++;
GrdTotal = GrdTotal + total;
label1.Text = "¥" + GrdTotal;
MessageBox.Show("书籍加入成功");
}
}
}
//-------------------------------------------------------------//
int key = 0, stock = 0;
private void printBtn_Click(object sender, EventArgs e)
{
printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pprnm", 400, 600);
if(printPreviewDialog1.ShowDialog()== DialogResult.OK)
{
printDocument1.Print();
}
}
int prodBookId, prodNum, prodPrice, total, pos = 60;
string prodName;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("匠心书店", new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, new Point(80,-20));
e.Graphics.DrawString("编号 产品 数量 价格 总结", new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, new Point(-30, 30));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
prodBookId = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn1"].Value);
prodName = "" + row.Cells["dataGridViewTextBoxColumn3"].Value;
prodPrice = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn8"].Value);
prodNum = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn9"].Value);
int.TryParse(row.Cells["dataGridViewTextBoxColumn4"].Value.ToString(), out total);
//decimal total = prodPrice * prodNum;
e.Graphics.DrawString("" + prodBookId, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(-30, pos));
e.Graphics.DrawString("" + prodName, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(0, pos));
e.Graphics.DrawString("" + prodNum, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(150, pos));
e.Graphics.DrawString("" + prodPrice, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(100, pos));
e.Graphics.DrawString("" + total, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(210, pos));
pos = pos + 20;
}
e.Graphics.DrawString("订单总额:¥" + GrdTotal, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Crimson, new Point(60, pos + 50));
e.Graphics.DrawString("------匠心书店------", new Font("微软雅黑", 10, FontStyle.Bold), Brushes.Crimson, new Point(40, pos + 85));
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
pos = 60;
GrdTotal = 0;
label1.Text = "¥0";
}
//加入订单
private void btnReset_Click_2(object sender, EventArgs e)
{
dataGridView1.Rows.Clear(); // 清空DataGridView中的数据
GrdTotal = 0; // 将订单总额重置为0
label1.Text = "¥" + GrdTotal; // 更新订单总额显示的Label文本
Reset();
}
}
}
首先,让我们来看看代码的运行顺序。当程序运行时,首先会执行 FrmBilling
类的构造函数 FrmBilling()
。该构造函数初始化了窗体并调用了 populate()
方法。
populate()
方法是连接数据库并将书籍信息展示在 DataGridView
中的关键方法。它使用了 SqlConnection
对象建立与数据库的连接,并执行查询语句从数据库中获取书籍信息。然后,通过 SqlDataAdapter
将查询结果填充到 DataSet
中,并将 DataSet
中的数据绑定到 DataGridView
控件上。
接下来,在窗体加载事件 FrmBilling_Load
中,启用了一个定时器 timer1
,每隔一秒钟更新一次状态栏中的当前时间。
在用户与界面交互过程中,当用户点击某一行书籍时,会触发 UGW_CellContentClick
事件。在这个事件中,首先获取所选书籍的相关信息,并显示在相应的文本框中。此外,还会获取书籍的库存量。
当用户点击“加入订单”按钮时,btnSave_Click
事件被触发。在这个事件中,首先会判断所选书籍的库存是否足够,如果不足则弹出提示信息。如果库存充足,则会判断订单中是否已经存在该书籍,如果存在,则更新数量和总价;如果不存在,则将该书籍添加到订单中,并更新总价。最后,弹出成功添加书籍的提示信息。
而当用户点击“打印订单”按钮时,printBtn_Click
事件被触发。在这个事件中,会弹出打印预览对话框,用户可以选择打印设置并预览订单。如果用户确认打印,则会调用 printDocument1_PrintPage
方法,将订单信息逐行打印出来,并清空订单数据,以便下次打印最新的订单。
另外,还有一些辅助方法和事件,如重置订单、定时器的 Tick 事件等,在文章中没有一一展开介绍。
2.下面是打印文档的一些代码,以及一些运行步骤,运行步骤我会写在下面
private void printBtn_Click(object sender, EventArgs e)
{
printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pprnm", 400, 600);
if(printPreviewDialog1.ShowDialog()== DialogResult.OK)
{
printDocument1.Print();
}
}
int prodBookId, prodNum, prodPrice, total, pos = 60;
string prodName;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("匠心书店", new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, new Point(80,-20));
e.Graphics.DrawString("编号 产品 数量 价格 总结", new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, new Point(-30, 30));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
prodBookId = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn1"].Value);
prodName = "" + row.Cells["dataGridViewTextBoxColumn3"].Value;
prodPrice = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn8"].Value);
prodNum = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn9"].Value);
int.TryParse(row.Cells["dataGridViewTextBoxColumn4"].Value.ToString(), out total);
//decimal total = prodPrice * prodNum;
e.Graphics.DrawString("" + prodBookId, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(-30, pos));
e.Graphics.DrawString("" + prodName, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(0, pos));
e.Graphics.DrawString("" + prodNum, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(150, pos));
e.Graphics.DrawString("" + prodPrice, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(100, pos));
e.Graphics.DrawString("" + total, new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Blue, new Point(210, pos));
pos = pos + 20;
}
e.Graphics.DrawString("订单总额:¥" + GrdTotal, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Crimson, new Point(60, pos + 50));
e.Graphics.DrawString("------匠心书店------", new Font("微软雅黑", 10, FontStyle.Bold), Brushes.Crimson, new Point(40, pos + 85));
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
pos = 60;
GrdTotal = 0;
label1.Text = "¥0";
}
当用户点击“打印订单”按钮时,会触发 printBtn_Click
事件,接下来会执行以下打印文档的运行步骤:
弹出打印预览对话框:在 printBtn_Click
事件中,会弹出一个打印预览对话框,该对话框可以显示待打印文档的预览效果,用户可以在此对话框中进行打印设置和预览。
打印设置:在打印预览对话框中,用户可以选择打印机、纸张大小、打印方向等打印设置。根据用户的选择,系统会记录相应的打印设置参数。
预览订单内容:在打印预览对话框中,系统会将订单的详细内容逐行展示,包括书籍名称、数量、单价等信息。用户可以通过滚动预览页面来查看完整的订单内容。
确认打印:如果用户满意预览结果,并决定进行打印,可以点击打印预览对话框中的“打印”按钮。
调用打印机进行打印:点击“打印”按钮后,系统会调用操作系统的打印功能,将待打印的文档发送给选择的打印机。根据之前记录的打印设置参数,打印机会按照用户的要求进行打印,将订单内容打印到纸张上。
打印完成:打印机完成打印后,会弹出打印机打印完成的提示。
3.忘了补上加入购物车的了
private void UGW_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && UGW.Columns[e.ColumnIndex].Name == "AddToCart") // 判断是否点击了加入购物车按钮列
{
string selectedTitle = UGW.Rows[e.RowIndex].Cells["Title"].Value.ToString();
int selectedPrice = Convert.ToInt32(UGW.Rows[e.RowIndex].Cells["Price"].Value);
int selectedStock = Convert.ToInt32(UGW.Rows[e.RowIndex].Cells["Stock"].Value);
if (txtNum.Text == "" || Convert.ToInt32(txtNum.Text) > selectedStock)
{
MessageBox.Show("库存不足!");
}
else
{
bool bookExists = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["BookTitle"].Value != null && row.Cells["BookTitle"].Value.ToString() == selectedTitle)
{
int selectedQuantity = Convert.ToInt32(txtNum.Text);
int currentQuantity = Convert.ToInt32(row.Cells["Quantity"].Value);
int totalQuantity = selectedQuantity + currentQuantity;
row.Cells["Quantity"].Value = totalQuantity;
int totalPrice = Convert.ToInt32(row.Cells["TotalPrice"].Value);
int total = selectedPrice * selectedQuantity;
row.Cells["TotalPrice"].Value = totalPrice + total;
GrdTotal += total;
label1.Text = "¥" + GrdTotal;
MessageBox.Show("书籍加入成功");
bookExists = true;
break;
}
}
if (!bookExists)
{
int selectedQuantity = Convert.ToInt32(txtNum.Text);
int total = selectedPrice * selectedQuantity;
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(dataGridView1);
newRow.Cells[0].Value = n + 1;
newRow.Cells[1].Value = selectedTitle;
newRow.Cells[2].Value = selectedPrice;
newRow.Cells[3].Value = selectedQuantity;
newRow.Cells[4].Value = total;
dataGridView1.Rows.Add(newRow);
n++;
GrdTotal += total;
label1.Text = "¥" + GrdTotal;
MessageBox.Show("书籍加入成功");
}
}
}
}
当用户在 DataGridView 中点击“加入购物车”按钮列时,触发了名为 UGW_CellContentClick 的事件处理程序。以下是此代码的大致运行步骤:
-
用户在 DataGridView 中点击“加入购物车”按钮列。
-
程序检查用户输入的数量是否为空或超过库存,如果是,则显示消息框提示“库存不足”。
-
如果用户输入的数量有效(不为空且未超过库存),程序将执行以下逻辑:
a. 遍历购物车中已有的商品,检查是否存在当前用户选择的商品。
b. 如果当前商品已存在于购物车中,则更新该商品的数量和总价,并更新购物车的总价。
c. 如果当前商品不存在于购物车中,则将新的商品信息添加到购物车中,并更新购物车的总价。
-
在上述逻辑执行完毕后,程序会根据用户操作的结果向用户显示相应的消息框提示,如“书籍加入成功”。
还有一部分未完功.....