I am writing a c# application. In the 'Inventory' Form is a datagridview which gets data from an SQL table 'Products'.
I have one form 'AddNew' which is adding some data with following code:
rivate void btnOk_Click(object sender, EventArgs e)
{
if (txtBarCode.Text == "" || txtProductName.Text == "" || txtPrice.Text == "" || numericUpDown1 == null)
{
MessageBox.Show("Please Fill", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Show();
txtBarCode.Focus();
return;
}
try
{
decimal price = Convert.ToDecimal(txtPrice.Text);
decimal awd;
awd = price - ((price * 18) / 100);
Program.AddProduct(txtBarCode.Text, txtArtNumber.Text, txtProductName.Text, txtPrice.Text, txtComment.Text, numericUpDown1.Value, awd.ToString(), txtSelfPrice.Text);
Inventary inv = new Inventary();
inv.save();
DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Inventary inv = new Inventary();
inv.load();
this.Close();
}
This code works fine but i want to make following : when user double clicks on a datagridview's cell it must open this the AddNew form, and pre-populate the texboxeswith data to edit.
How can I handle AddNew's btn ok click from the Inventory Form, using code like this
internal static void EditProduct(string txtBarCode, string txtArtNumber, string txtProductName, string txtPrice, string txtComment, decimal NumericUpDown1, string PriceWithOutAWD, string txtSelfPrice)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("UPDATE Products SET BarCode = @BarCode, ArtNumber = @ArtNumber, ProductName = @ProductName, Price = @Price, SelfPrice = @SelfPrice, PriceWithOutAWD = @PriceWithOutAWD, UnitsInStock = @UnitsInStock, Comment = @Comment WHERE BarCode = @bc AND ArtNumber = @an ", con);
cmd.Parameters.Add(new SqlParameter("@BarCode", txtBarCode));
cmd.Parameters.Add(new SqlParameter("@ArtNumber", txtArtNumber));
cmd.Parameters.Add(new SqlParameter("@ProductName", txtProductName));
cmd.Parameters.Add(new SqlParameter("@Price", txtPrice));
cmd.Parameters.Add(new SqlParameter("@SelfPrice", txtSelfPrice));
cmd.Parameters.Add(new SqlParameter("@PriceWithOutAWD", PriceWithOutAWD));
cmd.Parameters.Add(new SqlParameter("@UnitsInStock", NumericUpDown1));
cmd.Parameters.Add(new SqlParameter("@Comment", txtComment));
cmd.Parameters.Add(new SqlParameter("@bc", txtBarCode));
cmd.Parameters.Add(new SqlParameter("@an", txtArtNumber));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}