简体   繁体   English

C# 从另一个 Form2 更改一个 SQL 查询和一个 Form1 的 DGV

[英]C# Change a SQL query and a Form1's DGV from another Form2

I'm wondering what's the best way to change a variable from one Form to another.我想知道将变量从一种形式更改为另一种形式的最佳方法是什么。

I have in Form1 a datagridview that is updated by a sql query and listbox controls.我在 Form1 中有一个由 sql 查询和列表框控件更新的数据网格视图。 So, when I select items in my From1's listboxes, it changes the sql query that will update the DGV:因此,当我的 From1 列表框中的 select 项目时,它会更改将更新 DGV 的 sql 查询:

public void Defaultview()
    {
        string strSQL = "SELECT mycolumn1, mycolumn2 FROM myTable";
        string strWhere;
        string connetionString = @"Data Source=mydatasource;Initial Catalog=myDB;Integrated Security=SSPI";            
        strWhere = GetListFilter(tableLayoutPanelForm1);
            if (strWhere != null)
            {
                strSQL += " WHERE " + strWhere;
            }
        SqlConnection cnn = new SqlConnection(connetionString);
        SqlDataAdapter adapter= new SqlDataAdapter(strSQL, cnn);
        DataTable Dt = DataTable();
        adapter.Fill(Dt);
        dataGridView1.DataSource = Dt;
    }

GetListFilter function will loop through all my listboxes and update the strWhere in function of the selectedItems from the listboxes. GetListFilter function 将循环遍历我的所有列表框并更新列表框中所选项目的 function 中的 strWhere。

  public string GetListFilter(Control ctrlContainer)
    {
        string strWhere = null;

        foreach (Control ctrl in ctrlContainer.Controls)
        {
            string strCondition = null;
            // ListBox handling
            if (ctrl.GetType() == typeof(ListBox) && ctrl.Name.Contains("CFRA") == true)
            {
                ListBox lb = ctrl as ListBox;

                foreach (var li in lb.SelectedItems)
                {

                    strCondition += $"'{li}',";

                }
                if (strCondition != null)
                {
                    if (strWhere == null)
                    {
                        strCondition = strCondition.TrimEnd(',');
                        strWhere += $"{ctrl.Name.Substring(13)} IN ({strCondition})";
                    }
                    else if (strWhere != null)
                    {
                        strCondition = strCondition.TrimEnd(',');
                        strWhere += $" AND {ctrl.Name.Substring(13)} IN ({strCondition})";
                    }
                }
            }

        }
        return strWhere;
    }

Ok, now I click on a button1 from this Form1 and make a Form2 appears:好的,现在我单击此 Form1 中的 button1 并显示 Form2:

private void btn1(object sender, EventArgs e)
    {
        var Form2 = new Form2(this);
        Form2.Show();
        Form2.TopMost = true;
    }

Within this form2, I have two listboxes that I can drag and drop value from the first one to the other.在这个 form2 中,我有两个列表框,我可以将值从第一个拖放到另一个。 Then I have a button2 in this Form2 and when I click on it, I need to have my strSQL variable (from Form1) changed so that my DatagridView in Form1 can display the right infos.然后我在这个 Form2 中有一个 button2,当我点击它时,我需要更改我的 strSQL 变量(来自 Form1),以便我在 Form1 中的 DatagridView 可以显示正确的信息。

So my question is: At the end, only the string "strWhere" need to be updated with Form2's listbox selection so should I update my Form1's sql query from Form2?所以我的问题是:最后,只有字符串“strWhere”需要用 Form2 的列表框选择更新,所以我应该从 Form2 更新我的 Form1 的 sql 查询吗? Or maybe create a new datatable in Form2 that I send to my DGV in form1?或者也许在 Form2 中创建一个新的数据表,然后在 form1 中发送到我的 DGV? Or simply create a new instance of my Form1 when I click on the button of my Form2 but in this case, how can I take into acccount the new selection of my Form2... I'm a bit confused on how to proceed on this one... Any suggestion would be nice或者,当我单击 Form2 的按钮时,只需创建 Form1 的新实例,但在这种情况下,我该如何考虑 Form2 的新选择...我对如何继续进行此操作有点困惑一......任何建议都会很好

If I understand correctly, I think you need something like this at some point in Form2:如果我理解正确,我认为您在 Form2 中的某个时候需要这样的东西:

    Form1 form1;
    form1 = (Form1)Application.OpenForms["Form1"];
    if (form1 != null)
    {
        form1.UpdateStuff(var someParam);
    }

So, your Form1 is located in the application and then you invoke a public method (UpdateStuff) to do what you need.因此,您的 Form1 位于应用程序中,然后您调用公共方法 (UpdateStuff) 来执行您需要的操作。

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

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