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. So, when I select items in my From1's listboxes, it changes the sql query that will update the DGV:
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.
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:
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. 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.
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? Or maybe create a new datatable in Form2 that I send to my DGV in form1? 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
If I understand correctly, I think you need something like this at some point in 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.