简体   繁体   English

尝试从Form2调用Form1的方法时出现问题

[英]Problem when trying to call Form1`s methods from Form2

I'm using WinForms in C# 我在C#中使用WinForms

I have these methods in Inventory, I use these on Inventory_load event : 我在库存中有这些方法,在Inventory_load事件中使用了这些方法:

 ConstructTable();  GetProducts("");

And works fine but when im trying to use this methods from Form2 nothing happens when I call like this : 并且工作正常,但是当我尝试从Form2使用此方法时,我这样调用时没有任何反应:

 Inventory i = new Inventory();  i.ConstructTable();

 i.GetProducts(txtFind.Text.ToString());

These are the methods : 这些是方法:

public void ConstructTable()
        {
            ProductTable = new DataTable();

            ProductTable.Columns.Add(new DataColumn("ID", typeof(int)));
            ProductTable.Columns.Add(new DataColumn("BarCode", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("ArtNumber", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("ProductName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("Price", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("SelfPrice", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("PriceWithOutAWD",typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("TotalSelfPrice",typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("UnitsInStock", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("Comment", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("InputDateTime", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("InputQuantity", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("Margin", typeof(decimal)));
            ProductTable.Columns.Add(new DataColumn("CategoryName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("TypeName", typeof(string)));
            ProductTable.Columns.Add(new DataColumn("ExpDate", typeof(string)));

            ProductTable.TableName = "Products";
            ProductDataSet = new DataSet();
            ProductDataSet.Tables.Add(ProductTable);

            dgvInventory.DataSource = ProductDataSet;
            dgvInventory.DataMember = "Products";
        }

        public void GetProducts(string find)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("SELECT ID, BarCode, ArtNumber,ProductName, Price, SelfPrice, PriceWithOutAWD, TotalSelfPrice, UnitsInStock, " +
                                                    " Comment, InputDateTime, InputQuantity, Margin, CategoryName, TypeName, ExpDate FROM GetProducts"+
                                                    " WHERE BarCode LIKE @F OR ArtNumber LIKE @F OR ProductName LIKE @F OR Price LIKE @F OR Comment LIKE @F", 
                                                    new SqlConnection(Program.ConnectionString)))
                {
                    cmd.Parameters.AddWithValue("@F", "%" + find + "%");
                    cmd.Connection.Open();

                    SqlDataReader myReader = cmd.ExecuteReader();
                    while (myReader.Read())
                    {

                        ProductTable.Rows.Add
                            (
                            (int)myReader["ID"],
                            myReader["BarCode"].ToString(),
                            myReader["ArtNumber"].ToString(),
                            myReader["ProductName"].ToString(),
                            (decimal)myReader["Price"],
                            (decimal)myReader["SelfPrice"],
                            (decimal)myReader["PriceWithOutAWD"],
                            myReader["TotalSelfPrice"].ToString(),
                            myReader["UnitsInStock"].ToString(),
                            myReader["Comment"].ToString(),
                            myReader["InputDateTime"].ToString(),
                            myReader["InputQuantity"].ToString(),
                            myReader["Margin"].ToString(),
                            myReader["CategoryName"].ToString(),
                            myReader["TypeName"].ToString(),
                            myReader["ExpDate"].ToString()
                            );
                    }
                    cmd.Connection.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Program.MsgError1, "Acid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

I cannot seem to post a comment, so I will update my answer, you need to explain what exactly is not working. 我似乎无法发表评论,所以我将更新我的答案,您需要解释什么根本不起作用。

For example are you 100% sure that your code is correct? 例如,您是否100%确定您的代码正确? You have a similar question you have asked in the past about this code. 您过去也曾对此代码询问过类似的问题。

"I asked and get answer my code needed one space. I added it and its work fine on Inventory form but when i trying to use it from other form its doing nothing. thanks" “我问并得到答案,我的代码需要一个空格。我在库存表单中添加了它,并且它的工作正常,但是当我尝试从其他表单中使用它时却什么也没做。谢谢。”

Your methods are not static currently that is a major design flaw. 您的方法当前不是静态的,这是一个主要的设计缺陷。 Of course the design itself is flawed you might want to do research in the correct way to handle the data you are requesting. 当然,设计本身存在缺陷,您可能希望以正确的方式进行研究以处理您所请求的数据。

I can't tell what your second form ( called Inventory ) is suppose to do. 我无法告诉您第二种形式(称为Inventory)应该做什么。 Why are you not create the table before the form is created, and using the query as the source for the table, would be a much better design. 为什么不在创建表单之前就创建表,而将查询用作表的源则是一个更好的设计。

Your current method is extremely flawed and you clearly are having problems I suggest getting some co-worker support. 您当前的方法存在严重缺陷,您显然遇到了问题,我建议您获得同事的支持。

You should declare your methods static : 您应该将方法声明为static:

public static void ConstructTable()

and use them like : 并像这样使用它们:

Inventory.ConstructTable()

in Form2. 在Form2中。

It makes no architectural or design sense to have the ConstructTable() and GetProducts() methods with their current implementation as the codebehind on a winform . ConstructTable()GetProducts()方法及其当前实现作为winform上的代码隐藏没有任何架构或设计意义。

There is little or no cohesion between these methods and the visual representation of the data in your form, which is clearly illustrated by the fact that you end up calling code in one form from another. 这些方法与表单中数据的直观表示之间几乎没有凝聚力 ,或者以最终以一种形式调用另一种形式的代码这一事实清楚地说明了这一点。

Move this code into a class library and consider using Linq or some less archaic data access logic to retrieve your data. 将此代码移到类库中,并考虑使用Linq或一些不太古老的数据访问逻辑来检索您的数据。

With respect to the specific problem that you are experiencing I would recommend that you attach a debugger and trace through these lines of code. 关于您遇到的特定问题,我建议您附加一个调试器并通过这些代码行进行跟踪。 Pay attention to the value of the parameter being sent to GetProducts()! 注意发送到GetProducts()的参数的值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM