Click here to Skip to main content
15,891,006 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
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();
            }
        }
Posted
Updated 27-Oct-10 1:30am
v2
Comments
Keith Barrow 27-Oct-10 7:31am    
I fixed up your English a little.

1 solution

I am not Getting you Perfectly,

Try to use
DataGridView_CellContentDoubleClick 
Event.

put your code in this Event to show the Form.
 
Share this answer
 
Comments
lester555 27-Oct-10 8:57am    
Thanks. i know how to show that form when use click double in gridview but that forms buttonOk adding info to table i cant understood how to edit the same form table column

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900