Beispiel #1
0
        private void saveBtn_Click(object sender, EventArgs e)
        {
            CheckedListBox[] boxList = new CheckedListBox[5];
            boxList.SetValue(this.MeatListBox, 0);
            boxList.SetValue(this.FrozenListBox, 1);
            boxList.SetValue(this.GroceryListBox, 2);
            boxList.SetValue(this.ProduceListBox, 3);
            boxList.SetValue(this.EtcListBox, 4);
            DataGridView dataView = this.co.getOrderDataView();

            //use products to figure out deleted product
            for (int b = 0; b < boxList.Length; b++)
            {
                CheckedListBox listBox = boxList[b];
                for (int i = 0; i < listBox.CheckedItems.Count; i++)
                {
                    DataRowView item     = (DataRowView)listBox.CheckedItems[i];
                    String      prodName = (String)item.Row.ItemArray[1];
                    try
                    {
                        bool isFound = false;
                        for (int k = 0; k < dataView.Rows.Count; k++)
                        {
                            DataGridViewCellCollection cells = dataView.Rows[k].Cells;
                            if (cells[0].Value.Equals(prodName))
                            {
                                this.products.Remove(prodName);
                                isFound = true;
                                break;
                            }
                        }
                        if (!isFound)
                        {
                            db = new DbConnectorClass();
                            SqlDataReader dbReader = db.RunQuery("select * from dbo.product where product ='" + prodName + "';");

                            if (dbReader.Read())
                            {
                                int    box    = 0;
                                int    each   = 0;
                                int    pound  = 0;
                                String price  = "$" + db.NullToNA(dbReader, "price");
                                int    amount = 0;
                                String market = "";
                                String note   = db.NullToNA(dbReader, "note");
                                //add only new items
                                dataView.Rows.Add(prodName, box, each, pound, price, amount, market, this.routeValue, note);
                            }
                            dbReader.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            //remove unchecked item from order list
            for (int k = 0; k < this.products.Count; k++)
            {
                for (int u = 0; u < dataView.Rows.Count; u++)
                {
                    if (dataView.Rows[u].Cells[0].Value.Equals(this.products[k]))
                    {
                        dataView.Rows.RemoveAt(u);
                        break;
                    }
                }
            }
            this.Close();
        }