//Function Delete Type From database
        public bool DeleteType()
        {
            bool result = false;

            using (ConvenienceShopEntities entity = new ConvenienceShopEntities())
            {
                TypeOfProduct  a          = entity.TypeOfProducts.SqlQuery("select * from TypeOfProduct where Id=" + dgvTypeList.SelectedRows[0].Cells[0].Value.ToString()).FirstOrDefault();
                List <Product> lstProduct = new List <Product>();
                lstProduct = entity.Products.SqlQuery("select * from Product where TypeID=" + dgvTypeList.SelectedRows[0].Cells[0].Value.ToString()).ToList();
                foreach (Product x in lstProduct)
                {
                    List <BillInfo> lstBillInfo = new List <BillInfo>();
                    lstBillInfo = entity.BillInfoes.SqlQuery("select * from BillInfo where ProductID=" + x.Id).ToList();
                    foreach (BillInfo bill in lstBillInfo)
                    {
                        entity.BillInfoes.Remove(bill);
                        entity.SaveChanges();
                    }
                    entity.Products.Remove(x);
                    entity.SaveChanges();
                }
                entity.TypeOfProducts.Remove(a);
                entity.SaveChanges();
                result = true;
            }
            return(result);
        }
        //Function Add Type onto database
        public bool AddType(TypeOfProduct typeOfProduct)
        {
            bool result = false;

            using (ConvenienceShopEntities entity = new ConvenienceShopEntities())
            {
                entity.TypeOfProducts.Add(typeOfProduct);
                entity.SaveChanges();
                result = true;
            }
            return(result);
        }
        //Event Add type onto database
        private void btnAddType_Click(object sender,EventArgs e)
        {
            TypeOfProduct typeOfProduct = new TypeOfProduct();

            typeOfProduct.TypeName = txtTypeName.Text.Trim();

            bool result = AddType(typeOfProduct);

            if (result)
            {
                MessageBox.Show("Saved successfully!","Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Can not be saved!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            FormTypeManagement_Load(sender,e);
        }
        private void btnSearchType_Click(object sender,EventArgs e)
        {
            string query = txtSearchType.Text.Trim().ToLower();
            List <TypeOfProduct> data = new List <TypeOfProduct>();

            DisplayType();
            foreach (DataGridViewRow a in dgvTypeList.Rows)
            {
                if (a.Cells[0].Value.ToString().ToLower().Contains(query) ||
                    a.Cells[1].Value.ToString().ToLower().Contains(query))
                {
                    TypeOfProduct x = new TypeOfProduct();
                    x.Id       = Convert.ToInt32(a.Cells[0].Value);
                    x.TypeName = a.Cells[1].Value.ToString();

                    data.Add(x);
                }
            }

            dgvTypeList.DataSource = data;
        }