private void buttonToSport_Click(object sender, EventArgs e) { Methods tables = new Methods(); SportsAreUsEntities1 myData = new SportsAreUsEntities1(); //Get selected row(can click anywhere in the row) var selected = dataGridViewHealth.CurrentRow; int healthItemID = (int)selected.Cells["HealthItemID"].Value; var selectedHealthItem = (from item in myData.HealthItems where item.HealthItemID == healthItemID //Use the ID to match. select item).First(); //Must use unique value. SportingItem sportItem = new SportingItem { Name = selectedHealthItem.Name, Description = selectedHealthItem.Description, QuantityOnHand = selectedHealthItem.QuantityOnHand }; myData.SportingItems.Add(sportItem); myData.HealthItems.Remove(selectedHealthItem); myData.SaveChanges(); dataGridViewSports.DataSource = tables.RefreshGrids(1); dataGridViewHealth.DataSource = tables.RefreshGrids(2); }
public object RefreshGrids(int selectedData) { SportsAreUsEntities1 myData = new SportsAreUsEntities1(); if (selectedData == 1) { var query = from sportProduct in myData.SportingItems orderby sportProduct.SportingItemID ascending select new { sportProduct.SportingItemID, sportProduct.Name, sportProduct.Description, sportProduct.QuantityOnHand }; return(query.ToList()); } else if (selectedData == 2) { var query = from healthProduct in myData.HealthItems orderby healthProduct.HealthItemID ascending select new { healthProduct.HealthItemID, healthProduct.Name, healthProduct.Description, healthProduct.QuantityOnHand }; return(query.ToList()); } else { throw new System.ArgumentException("Data Out of Range"); } }