private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value); bool value = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[4].Value); Inkop inkop = new Inkop(); inkop.id = id; inkop.purchased = value; MySQLDb.UpdatePurchasedInkop(inkop); }
private void button1_Click(object sender, EventArgs e) { if (textBoxWhat.Text == "" || textBoxQuantity.Text == "") { MessageBox.Show("Noob! Du måste ju skriva nåt i båda textfälten!"); } else { Inkop inkop = new Inkop(); inkop.article = textBoxWhat.Text; inkop.quantity = textBoxQuantity.Text; inkop.purchased = false; MySQLDb.SubmitInkop(inkop); List<Inkop> inkopslista = MySQLDb.GetAllInkop(); inkopslistaBinding = new BindingList<Inkop>(inkopslista); dataGridView1.DataSource = inkopslistaBinding; } }
public static void SubmitInkop(Inkop inkop) { ConnectToCafeDb(); string sql = "SELECT MAX(id) FROM `shoppinglist`"; MySqlCommand cmd = new MySqlCommand(sql, conn); object result = cmd.ExecuteScalar(); int newId = Convert.ToInt32(result) + 1; //Hämta högsta id och öka med 1 string purchased = inkop.purchased ? "yes" : "no"; sql = "INSERT INTO `shoppinglist` (id, date, articleName, quantity, purchase) VALUES (\'" + newId + "\', CURDATE(), \'" + inkop.article + "\', \'" + inkop.quantity + "\', \'" + purchased + "\')"; cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); CloseConnection(); }
public static void UpdatePurchasedInkop(Inkop inkop) { ConnectToCafeDb(); MySqlCommand cmd; string purchaseString = inkop.purchased ? "yes" : "no"; string sql = "UPDATE `shoppinglist` SET purchase = \'" + purchaseString + "\' WHERE id = " + inkop.id; cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); CloseConnection(); }
public static List<Inkop> GetAllInkop() { List<Inkop> inkopslist = new List<Inkop>(); ConnectToCafeDb(); string sql = "SELECT * FROM `shoppinglist` ORDER BY id DESC"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); Inkop inkop; string temp; while (rdr.Read()) { inkop = new Inkop(); inkop.id = rdr.GetInt32(0); inkop.date = rdr.GetDateTime(1); inkop.article = rdr.GetString(2); inkop.quantity = rdr.GetString(3); temp = rdr.GetString(4); inkop.purchased = temp == "yes"; inkopslist.Add(inkop); } rdr.Close(); CloseConnection(); return inkopslist; }