我想解决这个问题 [英] I'm tyring to solve this problem

查看:74
本文介绍了我想解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#应用程序.在库存"表单中是一个datagridview,它从SQL表产品"中获取数据.
我有一种形式"AddNew",它使用以下代码添加一些数据:

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();
        }



这段代码运行良好,但是我想做以下事情:当用户双击datagridview的单元格时,它必须打开此AddNew表单,并用数据预填充texboxes进行编辑.

如何使用类似这样的代码从库存表单处理AddNew的btn ok点击



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();
            }
        }

推荐答案

我不能完美地帮助您,

尝试使用
I am not Getting you Perfectly,

Try to use
DataGridView_CellContentDoubleClick 

事件.

将您的代码放在此事件中以显示表单.

Event.

put your code in this Event to show the Form.


这篇关于我想解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆