C#初学小项目—WinForm图书管理系统8

8、用户登录后选购图书界面

页面展示

在这里插入图片描述
打印小票
在这里插入图片描述

功能描述

用户成功登录后跳转到图书选购页面,页面左上角会显示用户名称,左下角有退出登录,点击会退出到登录页面。书籍列表显示数据库中所有图书的详细数据,包括书名,作者,书籍类型,库存数量和单价。用户可以通过点击列表中图书来显示到上方的选购框,选购框中书名和价格无法修改,数量可以修改,默认数量为1,点击加入购物车按钮,将选购的图书添加到右侧的购物车列表,添加到购物车后,书籍列表的数量会实时更新。可以通过重置按钮清空选购框重新选择。购物车列表显示书名,单价,数量和总额,用户添加购物车的图书会依次显示该表中,且右下角会实时显示消费总额。选购完毕后,点击结算按钮打印小票,完成购物。

功能实现及关键代码

连接数据库

SqlConnection Con = new SqlConnection(@"");
// 填充函数
private void populate()
{
    Con.Open();
    string query = "select * from BookTb1";
    SqlDataAdapter adapter = new SqlDataAdapter(query, Con);
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
    var ds = new DataSet();
    adapter.Fill(ds);
    BookGDV.DataSource = ds.Tables[0];
    Con.Close();
}

更新函数,加入购物车后数量相应减少

private void ReSet()
{
    int newQty = stock - Convert.ToInt32(AmountTB.Text);
    try
    {
        Con.Open();
        string query = "update BookTb1 set BQty = '"+newQty+"' where BId = '"+key+"'";
        SqlCommand cmd = new SqlCommand(query,Con);
        cmd.ExecuteNonQuery();
        // MessageBox.Show("已加入购物车!");
        Con.Close();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

点击列表中书籍,显示到上方文本框中

int stock = 0, key = 0;
private void BookGDV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    BNameTB.Text = BookGDV.SelectedRows[0].Cells[1].Value.ToString();
    PriceTB.Text = BookGDV.SelectedRows[0].Cells[5].Value.ToString();
    // AmountTB.Text = BookGDV.SelectedRows[0].Cells[4].Value.ToString();
    AmountTB.Text = "1";
    if (BNameTB.Text == "")
    {
        stock = 0;
        key = 0;
    }
    else
    {
        stock = Convert.ToInt32(BookGDV.SelectedRows[0].Cells[4].Value.ToString());
        key = Convert.ToInt32(BookGDV.SelectedRows[0].Cells[0].Value.ToString());
    }
    
}

加入购物车功能

int n = 0, money = 0;

// 加入购物车
private void SaveBtn_Click(object sender, EventArgs e)
{
    if(AmountTB.Text == "" || Convert.ToInt32(AmountTB.Text) > stock)
    {
        MessageBox.Show("库存不足!!!");
    }
    else
    {
        int total = Convert.ToInt32(AmountTB.Text) * Convert.ToInt32(PriceTB.Text);
        // 零时数据库存放
        DataGridViewRow dr = new DataGridViewRow();
        dr.CreateCells(BillGDV);
        dr.Cells[0].Value = n + 1;
        dr.Cells[1].Value = BNameTB.Text;
        dr.Cells[2].Value = PriceTB.Text;
        dr.Cells[3].Value = AmountTB.Text;
        dr.Cells[4].Value = total;
        BillGDV.Rows.Add(dr);
        n += 1;
        ReSet();
        populate();
        clear();
        money = money + total;
        MoneyTB.Text = money.ToString() + "元";
    }
}

结算打印功能

private void PrintBtn_Click(object sender, EventArgs e)
{
    if (BillGDV.Rows[0].Cells[0].Value == null)
    {
        MessageBox.Show("您还没有挑选书籍~~~");
    }
    else
    {
        printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pprnm", 285, 600);
        if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
        try
        {
            Con.Open();
            string query = "insert into BillTb1 values('" + UserNameLbl.Text + "','" + money + "')";
            SqlCommand cmd = new SqlCommand(query, Con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("订单信息保存成功!");
            Con.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
}

绘制打印内容,最终打印显示,建议直接抄

 int prodid, prodpty, prodprice, tottal, pos = 80;

 private void Billing_Load(object sender, EventArgs e)
 {
     UserNameLbl.Text = login.UserName;
 }

 string prodname;
 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
 {
     e.Graphics.DrawString("如意书屋",new Font("幼圆",12,FontStyle.Bold),Brushes.Pink,new Point(100,30));
     e.Graphics.DrawString("编号   产品    价格    数量  总计", new Font("幼圆", 10, FontStyle.Bold), Brushes.Pink, new Point(26,60));
     foreach (DataGridViewRow row in BillGDV.Rows)
     {
     	// 注意对应表中列的命名
         prodid = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn1"].Value);
         prodname = "" + row.Cells["dataGridViewTextBoxColumn2"].Value;
         prodprice = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn3"].Value);
         prodpty = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn4"].Value);
         tottal = Convert.ToInt32(row.Cells["dataGridViewTextBoxColumn5"].Value);
         e.Graphics.DrawString("" + prodid, new Font("宋体", 8, FontStyle.Bold), Brushes.Black, new Point(26,pos));
         e.Graphics.DrawString("" + prodname, new Font("宋体", 8, FontStyle.Bold), Brushes.Black, new Point(70, pos));
         e.Graphics.DrawString("" + prodprice, new Font("宋体", 8, FontStyle.Bold), Brushes.Black, new Point(140, pos));
         e.Graphics.DrawString("" + prodpty, new Font("宋体", 8, FontStyle.Bold), Brushes.Black, new Point(200, pos));
         e.Graphics.DrawString("" + tottal, new Font("宋体", 8, FontStyle.Bold), Brushes.Black, new Point(240, pos));
         pos = pos + 20;
     }
     e.Graphics.DrawString("订单总额:¥" + MoneyTB.Text, new Font("宋体", 12, FontStyle.Bold), Brushes.Pink, new Point(60,pos+50));
     e.Graphics.DrawString("*************如意书屋*************", new Font("宋体", 12, FontStyle.Bold), Brushes.Pink, new Point(0, pos + 85));
     BillGDV.Rows.Clear();
     BillGDV.Refresh();
     pos = 100;
     MoneyTB.Text = null;
 }

感谢您的阅读,欢迎讨论批评!

  • 34
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如意~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值