コード例 #1
0
        private void But_Click1(object sender, EventArgs e)
        {
            bool isEmpty = !lt.Any();
            bool check   = false;

            if (isEmpty)
            {
                Model1 db  = new Model1();
                double gia = 0;

                foreach (Drink j in db.Drink)
                {
                    if (j.id_Drink == Convert.ToInt32(((Button)sender).Tag))
                    {
                        gia = j.price;
                    }
                }
                lt.Add(new Cart
                {
                    Id      = Convert.ToInt32(((Button)sender).Tag),
                    NameSp  = ((Button)sender).Name.ToString(),
                    SoLuong = 1,
                    Tien    = gia,
                });
                double total = 0;
                foreach (Cart i in lt)
                {
                    total += i.Tien;
                }
                textBox1.Text            = total.ToString();
                dataGridView1.DataSource = null;
                var l = lt.Select(p => new { p.NameSp, p.SoLuong, p.Tien });
                dataGridView1.DataSource = l.ToList();;
            }
            else
            {
                Model1 db  = new Model1();
                double gia = 0;
                foreach (Drink j in db.Drink)
                {
                    if (j.id_Drink == Convert.ToInt32(((Button)sender).Tag))
                    {
                        gia = j.price;
                    }
                }
                for (int i = 0; i <= lt.Count - 1; i++)
                {
                    if (String.Compare(lt[i].Id.ToString(), ((Button)sender).Tag.ToString(), true) == 0)
                    {
                        lt[i].SoLuong += 1;
                        lt[i].Tien     = gia * lt[i].SoLuong;
                        check          = true;
                    }
                }
                if (check == false)
                {
                    lt.Add(new Cart
                    {
                        Id      = Convert.ToInt32(((Button)sender).Tag),
                        NameSp  = ((Button)sender).Name.ToString(),
                        SoLuong = 1,
                        Tien    = gia,
                    });
                }
                double total = 0;
                foreach (Cart i in lt)
                {
                    total += i.Tien;
                }
                textBox1.Text            = total.ToString();
                dataGridView1.DataSource = null;
                var l = lt.Select(p => new { p.NameSp, p.SoLuong, p.Tien });
                dataGridView1.DataSource = l.ToList();;
            }
        }