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;
 }

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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-15 10:20:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-15 10:20:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-15 10:20:02       87 阅读
  4. Python语言-面向对象

    2024-04-15 10:20:02       96 阅读

热门阅读

  1. 高级IO——React服务器简单实现

    2024-04-15 10:20:02       44 阅读
  2. 将图片数据转换为张量(Go并发处理)

    2024-04-15 10:20:02       47 阅读
  3. go第三方库go.uber.org介绍

    2024-04-15 10:20:02       42 阅读
  4. 前后端AES对称加密 前端TS 后端Go

    2024-04-15 10:20:02       55 阅读
  5. 文件上传下载

    2024-04-15 10:20:02       32 阅读
  6. obs二开_播放媒体源

    2024-04-15 10:20:02       33 阅读
  7. kafka---broker相关配置

    2024-04-15 10:20:02       37 阅读
  8. WPF中Binding的原理和应用

    2024-04-15 10:20:02       38 阅读
  9. data_process11

    2024-04-15 10:20:02       32 阅读
  10. 解决 assemble 长时间卡死并失败问题

    2024-04-15 10:20:02       37 阅读