I have a gridview with a delete button. I’ve added a messagebox with an ‘are you sure’ yes/no. The row of data is deleted fine when you click yes, but the gridview data disappears if you click no.
Here’s the code for the delete button click:
private void gvOrderLines_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int intOrderID = 0;
if (e.RowIndex < 0 || e.ColumnIndex != gvOrderLines.Columns["deleteBtn"].Index) return;
Int32 orderLineID = (Int32)gvOrderLines[1, e.RowIndex].Value;
DialogResult result = MessageBox.Show("Do you want to delete this item? ","Delete Item",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand comm;
comm = new SqlCommand("del_OrderLine", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@orderLineID", SqlDbType.Int));
comm.Parameters["@orderLineID"].Value = orderLineID;
comm.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
try
{
conn.Open();
comm.ExecuteNonQuery();
intOrderID = (int)comm.Parameters["@ReturnValue"].Value;
}
catch
{
//tba
}
finally
{
comm.Dispose();
conn.Close();
}
}
}
populateOrderLines(intOrderID); // This repopulates the gv so why is it blank?
populateOrderTotals(intOrderID);
}
Any suggestions?
All the best,
Numb
Put these lines within the
if (result == DialogResult.Yes)block: