I just want to ask how I can update or change the cell value of column 'quantity' in my DataGridView when I select a certain row.
I have 3 columns in my DataGridView which are Item Name
, Quantity
and Price
.
Every time I click the button 'add' or 'less' on my form, the quantity of the selected row item will either add to or subtract 1 from the quantity column and the price will also update every time the quantity increments or decrements. Here is my code when I add my item to my DataGridView.
I'm not really sure if I got it right to add items to my datagrid.
cmd = new MySqlCommand("SELECT * FROM tblmenu", dbConn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Button btn = new Button();
btn.Text = rdr["menuName"].ToString();
btn.Width = 126;
btn.Height = 80;
btn.Click += delegate
{
MySqlConnection cnn2 = new MySqlConnection(sqlConn.connString);
cnn2.Open();
cmd = new MySqlCommand("SELECT menuName, menuPrice FROM tblmenu WHERE menuName = @name", cnn2);
cmd.Parameters.AddWithValue("@name", btn.Text);
MySqlDataReader rdr2 = cmd.ExecuteReader();
while (rdr2.Read())
{
dataGridView1.Rows.Add(rdr2.GetString("menuName").ToUpper(), 1, rdr2.GetDouble("menuPrice"));
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
lblQty.Text = dataGridView1.RowCount.ToString();
};
}
I have tried this but when I click another set of item, the quantity from the previous selected item still increments.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Add.Click += delegate
{
dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) + 1;
};
Minus.Click += delegate
{
dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) - 1;
};
}