续接超市收银(2)

先上效果图

上图是刚运行的界面

下面是手动添加商品界面

下图是生成清单界面展示

 代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GoodsForm
{
  //
    public partial class ProductForm : Form
    {
    
        ComboBox cbo = new ComboBox();
        TextBox tb = new TextBox();
        public static DataTable dt = new DataTable("Table_ProductForm");
        private int _i = 1;
        Goods mi = new Goods() { name = "米", price = 100 };
        Goods mian = new Goods() { name = "面", price = 200 };
        Goods you = new Goods() { name = "油", price = 300 };
        Goods guo = new Goods() { name = "锅", price = 200 };
        Goods cu = new Goods() { name = "醋", price = 400 };
        Goods jiu = new Goods() { name = "酒", price = 500 };
        Goods yan = new Goods() { name = "盐", price = 600 };
        public ProductForm()
        {
            InitializeComponent();
            InitDataGrideView2();
            CreateDataTable();
            this.dataGridView2.CellEnter += dataGridView2_CellEnter;
            this.dataGridView2.EditingControlShowing += dataGridView2_EditingControlShowing;
            timer1.Interval = 300;
            timer1.Start();
        }

        /// <summary>
        /// 实现单击一次显示下拉列表框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
            {
                SendKeys.Send("{F4}");
            }
        }
        /// <summary>
        /// 下拉列表编辑控制展示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string productName = dataGridView2.CurrentCell.OwningColumn.Name;

            if (e.Control is ComboBox)
            {
                if (productName == "colName" && dataGridView2.CurrentCell.RowIndex != -1)
                {
                    cbo = e.Control as ComboBox;
                    if (cbo != null)
                    {
                        cbo.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
                    }
                    cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
                }
                if (productName == "colDiscount" && dataGridView2.CurrentCell.RowIndex != -1)
                {
                    cbo = e.Control as ComboBox;
                    if (cbo != null)
                    {
                        cbo.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
                    }
                    cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged1);
                }
            }
        }

        /// <summary>
        /// 商品名称列表显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbo_SelectedIndexChanged(object sender, EventArgs e )
        {
            ComboBox combox = sender as ComboBox;
            int rowid = this.dataGridView2.CurrentRow.Index;
           
            if (combox.Text == "米")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "100.00";
                dataGridView2.CurrentRow.Cells[3].Value = "100.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "米";
                dt.Rows[rowid]["原价"] = 100.00;
                dt.Rows[rowid]["现价"] = 100.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
            if (combox.Text == "面")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "200.00";
                dataGridView2.CurrentRow.Cells[3].Value = "200.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "面";
                dt.Rows[rowid]["原价"] = 200.00;
                dt.Rows[rowid]["现价"] = 200.00;
                dt.Rows[rowid]["优惠模式"] = null;

            }
            if (combox.Text == "油")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "300.00";
                dataGridView2.CurrentRow.Cells[3].Value = "300.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "油";
                dt.Rows[rowid]["原价"] = 300.00;
                dt.Rows[rowid]["现价"] = 300.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
            if (combox.Text == "锅")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "200.00";
                dataGridView2.CurrentRow.Cells[3].Value = "200.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "锅";
                dt.Rows[rowid]["原价"] = 200.00;
                dt.Rows[rowid]["现价"] = 200.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
            if (combox.Text == "醋")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "400.00";
                dataGridView2.CurrentRow.Cells[3].Value = "400.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "醋";
                dt.Rows[rowid]["原价"] = 400.00;
                dt.Rows[rowid]["现价"] = 400.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
            if (combox.Text == "酒")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "500.00";
                dataGridView2.CurrentRow.Cells[3].Value = "500.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "酒";
                dt.Rows[rowid]["原价"] = 500.00;
                dt.Rows[rowid]["现价"] = 500.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
            if (combox.Text == "盐")
            {
                dataGridView2.CurrentRow.Cells[2].Value = "600.00";
                dataGridView2.CurrentRow.Cells[3].Value = "600.00";
                dataGridView2.CurrentRow.Cells[4].Value = null;

                dt.Rows[rowid]["商品名称"] = "盐";
                dt.Rows[rowid]["原价"] = 600.00;
                dt.Rows[rowid]["现价"] = 600.00;
                dt.Rows[rowid]["优惠模式"] = null;
            }
        }
        /// <summary>
        /// 优惠模式列表显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbo_SelectedIndexChanged1(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            int rowId = this.dataGridView2.CurrentRow.Index;
            if (combox.Text == "None") 
            {
                double temp = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells[2].Value);
                (this.dataGridView2.CurrentRow.Cells[3] as DataGridViewTextBoxCell).Value = string.Format("{0}", temp);

                dt.Rows[rowId]["现价"] = temp;
                dt.Rows[rowId]["优惠模式"] = "None";
                return;
            }
            if (combox.Text == "打九折")
            {
                double temp = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells[2].Value);
                (this.dataGridView2.CurrentRow.Cells[3] as DataGridViewTextBoxCell).Value = string.Format("{0}", temp * 0.9);

                dt.Rows[rowId]["现价"] = temp * 0.9;
                dt.Rows[rowId]["优惠模式"] = "打九折";
            }
            if (combox.Text == "满减(满100减20)")
            {
                double temp = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells[2].Value);
                if (temp >= 100)
                {
                    (this.dataGridView2.CurrentRow.Cells[3] as DataGridViewTextBoxCell).Value = string.Format("{0}", temp - 20);
                    dt.Rows[rowId]["现价"] = temp - 20;
                    dt.Rows[rowId]["优惠模式"] = "满减(满100减20)";
                }
            }
            if (combox.Text == "满减+打折")
            {
                double temp = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells[2].Value);
                if (temp >= 100)
                {
                    double temp1 = temp - 20;
                    (this.dataGridView2.CurrentRow.Cells[3] as DataGridViewTextBoxCell).Value = string.Format("{0}", temp1 * 0.9);
                    dt.Rows[rowId]["现价"] = temp1 * 0.9;
                    dt.Rows[rowId]["优惠模式"] = "满减+打折";
                }
            }
        }
        /// <summary>
        /// 优惠价格统计
        /// </summary>
        public double ProductDiscount_Display()
        {
            double oriPrice = 0;
            double curPrice = 0;
            double discount = 0;
            int count = dt.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                if (dt.Rows.Count == 0)
                {
                    oriPrice = 0.00;
                    curPrice = 0.00;
                }
                if (dt.Rows.Count > 0)
                {
                    oriPrice += Convert.ToDouble(dt.Rows[i][2].ToString());
                    curPrice += Convert.ToDouble(dt.Rows[i][3].ToString());
                }
            }
            discount = oriPrice - curPrice;
            return discount;
        }
        /// <summary>
        /// 统计商品原来价格
        /// </summary>
        /// <returns></returns>
        public double ProductOriPrice_Display()
        {
            double total = 0;
            int count = dt.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                if (dt.Rows.Count == 0)
                {
                    total += 0.00;
                }
                if (dt.Rows.Count > 0)
                {
                    total += Convert.ToDouble(dt.Rows[i][2].ToString());
                }
            }
            return total;
        }

        /// <summary>
        /// 统计商品现在价格
        /// </summary>
        /// <returns></returns>
        public double ProductCurPrice_Display()
        {
            double curPrice = 0;
            int count = dt.Rows.Count;
            for (int i = 0;i < count;i++)
            {
                if (dt.Rows.Count == 0)
                {
                    curPrice += 0.00;
                }
                if (dt.Rows.Count > 0)
                {
                    curPrice += Convert.ToDouble(dt.Rows[i][3].ToString());
                }
            }
            return curPrice;
        }
        
        /// <summary>
        /// 设置数据源属性
        /// </summary>
        private void InitDataGrideView2()
        {
            this.colIndex.DataPropertyName = "序号";
            this.colName.DataPropertyName = "商品名称";
            this.colOriPrice.DataPropertyName = "原价";
            this.colCurPrice.DataPropertyName = "现价";
            this.colDiscount.DataPropertyName = "优惠模式";

            this.dataGridView2.Columns[0].ReadOnly = true;
            this.dataGridView2.Columns[2].ReadOnly = true;
            this.dataGridView2.Columns[3].ReadOnly = true;
        }

        /// <summary>
        /// 创建DataTable表格
        /// </summary>
        private void CreateDataTable()
        {
            dt.Columns.Add("序号", typeof(int));
            dt.Columns.Add("商品名称", typeof(string));
            dt.Columns.Add("原价", typeof(double));
            dt.Columns.Add("现价", typeof(double));
            dt.Columns.Add("优惠模式", typeof(string));
        }

       /// <summary>
       /// 添加商品
       /// </summary>
       /// <returns></returns>
        protected DataTable AddDataRow()
        {
            DataRow row = dt.NewRow();
            row["序号"] = _i;
            row["商品名称"] = null;
            row["原价"] = 0.00;
            row["现价"] = 0.00;
            row["优惠模式"] = null;
            _i++;
            dt.Rows.Add(row);
            return dt;
        }

        /// <summary>
        /// 添加商品按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Add_Click(object sender, EventArgs e)
        {
            AddDataRow();
            dataGridView2.Rows.Add();
            int cur = dataGridView2.Rows.Count;
            dataGridView2.Rows[cur - 1].Cells[2].Value = "0.00";
            dataGridView2.Rows[cur - 1].Cells[3].Value = "0.00";
        }
       
        /// <summary>
        /// 刷新优惠、总价
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer1_Tick(object sender, EventArgs e)
        {
           
            this.OriPrice.Text = ProductOriPrice_Display().ToString();
            this.ProductDiscount.Text = ProductDiscount_Display().ToString();
            this.CurPrice.Text = ProductCurPrice_Display().ToString();
        }

        /// <summary>
        /// 输出商品清单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Out_Form_Click(object sender, EventArgs e)
        {
            //this.Hide();//隐藏窗口
           

            GoodsFormView goodsFormView = new GoodsFormView();
            goodsFormView.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;//把此窗体显示屏幕中央

            goodsFormView.dt = dt;

            goodsFormView.productOriPrice = ProductOriPrice_Display();
            goodsFormView.productDiscount = ProductDiscount_Display();
            goodsFormView.productCurPrice = ProductCurPrice_Display();
            goodsFormView.Show();
        }
        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 删除选中行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            for(int i=dataGridView2.SelectedRows.Count;i>0; i--)
            {
                int a =  dataGridView2.SelectedRows[i - 1].Index;//删除所选中行对应的数据
                dt.Rows.RemoveAt(a);
                dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i - 1].Index);
                dataGridView2.Refresh();
            }
           // dataGridView2.Refresh();//更新
        }
        /// <summary>
        /// 处理表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Handle_view(object sender, EventArgs e)
        {



        }

        private void dataGridView2_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            int cur = e.RowIndex;
            dataGridView2.Rows[cur].Cells[0].Value = (cur + 1).ToString();
        }
        // public DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit

        public DataGridViewTextBoxEditingControl CellEdit = null;

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView2.CurrentCellAddress.X == 4)
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                CellEdit.SelectAll();
                CellEdit.KeyPress += Cells_KeyPress; //绑定事件
            }
        }
        private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
        {
            if ((this.dataGridView2.CurrentCellAddress.X == 4) || (this.dataGridView2.CurrentCellAddress.X == 5) || (this.dataGridView2.CurrentCellAddress.X == 6))
            {

                //只能输入整数, 无法输入小数
                if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
                if (e.KeyChar == '\b') e.Handled = false;
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ProductForm_Load(object sender, EventArgs e)
        {
        }

        private void OriPrice_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

    }
}

控件名称不一一展示了,大致就是这样,一个面向对象的题目做成面向过程的了,后期改的话,目前实现不了封装,重新做吧。大家仅供参考。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值