-1

I would like to update a panel in a windows form C# after adding an entity from another form and without having to use Application.Restart();

Form 1 :

    private void kopf1_Load(object sender, EventArgs e)
    {
        kopf1.PopulateEntladeNr();

    }

Form 2:

    private void button1_Click(object sender, EventArgs e)
    {
        // command object identifying the stored procedure
        switch (MessageBox.Show("Are you sure you want to save this?",
          "Verify",
          MessageBoxButtons.YesNoCancel,
          MessageBoxIcon.Question))
        {

            case DialogResult.Yes:

                // "Yes" processing
                {
                    Insert_WE();
                    Application.Restart(); 
                }
                break;

So basically after clicking on a button I will find the information added in the combobox of the main form as if I did restart the app. Bare in mind that f1.update() didn't work. So could someone help please?

  • Do you have any code you've written – Twyxz Mar 14 '18 at 13:34
  • Well in Form main I have : private void kopf1_Load(object sender, EventArgs e) { kopf1.PopulateEntladeNr(); } – Linda Pavlović Mar 14 '18 at 13:36
  • and on the other form I generate an EntladeNr if there is none. And my issue is how do I add it in the main form after generating it without : **Application.Restart()** – Linda Pavlović Mar 14 '18 at 13:38
  • So essentially you're trying to get data to display in your original form without restarting? – Twyxz Mar 14 '18 at 13:39
  • @Twyxz Exactly! – Linda Pavlović Mar 14 '18 at 13:43
  • Show what code you've already written by editing your question – Twyxz Mar 14 '18 at 13:43
  • If u added items to a `combobox` control,you don't need to do the so-called `Refresh`thing....If ,for example,your situation is a bit different,like u are adding data to a database and a `DatagridView` already has a binding to it,then ,in order to get the latest data,you can re-set the databinding or just manually add rows to the dgvw containing the same data you inserted in your database..However,there's a method called `Refresh` and you can call it like `Me.Refresh` but that just does the paint job for you ..Hope this helps :) – Software Dev Mar 14 '18 at 13:44
  • @Twyxz I edited the question :) – Linda Pavlović Mar 14 '18 at 13:51
  • Okay so if someone verifies it you want it back on the main form in a combo box eg; I enter my name and I verify then my name appears in the combo box without having to restart? – Twyxz Mar 14 '18 at 13:52
  • @zackraiyan Thank you for the answer , but the thing is that the combobox stays the same after adding the EntladeNr and also those Information are sent to the database then taken later as you did mention. But not from a datagridView, I don't know if it's more clear now.. – Linda Pavlović Mar 14 '18 at 13:54
  • @Twyxz That's right! – Linda Pavlović Mar 14 '18 at 13:55
  • The edit changes the whole thing...where did a `panel` come from ? – Software Dev Mar 14 '18 at 13:55
  • @zackraiyan So the panel is **Kopf** and it is located in the main form. It has a button **Add EntladeNr** when I click on it another form appears. I add the infomation needed and save.After saving I want the ID of the new EntladeNr to be added in the combobox I am mentioning. – Linda Pavlović Mar 14 '18 at 13:58
  • So youre going back to main form without restarting? Does the combo box work? Like does the data go into it or not – Twyxz Mar 14 '18 at 13:59
  • @Twyxz exactly I go back without restarting and it doesn't add the new EntladeNr that I just created. But when I do restart it obviously does it as the request is re-called. – Linda Pavlović Mar 14 '18 at 14:01
  • Oh okay so basically you're having trouble executing the code that fills the combo box once program is being ran and you need it to populate as you add Entlade – Twyxz Mar 14 '18 at 14:03
  • Can you show the code on the combo box – Twyxz Mar 14 '18 at 14:03
  • That's kinna impossible......maybe,if i assume correct,your combobox has binding to a database and u are adding data to the database rather than the combobox,right ? – Software Dev Mar 14 '18 at 14:05
  • just as i suspected,you are rather adding data to the db and the combobox only has a binding...lol,you should've mentioned tht earlier – Software Dev Mar 14 '18 at 14:10
  • public void PopulateEntladeNr() { string cmd = @"SQLRequest" SqlDataReader dataRead = myConnection.DataReader(cmd); while (dataRead.Read()) { EntladeNr_Combo.Items.Add(dataRead["EntladeNr"]); } – Linda Pavlović Mar 14 '18 at 14:10
  • pleas wait...i am trying to write an answer – Software Dev Mar 14 '18 at 14:15
  • Have you not tried using the method to populate the combo box within the button press instead – Twyxz Mar 14 '18 at 14:26
  • @Twyxz I did this : { Insert_WE(); Form1 F; F.DataReset(); } But what happens is that the form1 restarts – Linda Pavlović Mar 14 '18 at 14:38

2 Answers2

0

I am not sure of what you are asking.But as far my assumptions,if you are doing this in your Form2 :

Form1.ComboBox1.Items.Add(mysringhere)

Then it should definitely add items to the combobox.However,from your comments,i see that u are adding items to the combobox within the While loop of the dataReader,it would do the same.However,you can give this a try

 Form1.Combobox1.Items.Add(dataRead(3)).ToString;

Replace 3 with your column/cell number and use .ToString to make sure the data is passed as a string

Hope this helps :)

Software Dev
  • 5,368
  • 5
  • 22
  • 45
0

I did solve this by making the main form accessible in the second form.

   if (System.Windows.Forms.Application.OpenForms["Form1"] != null)
                    {
                        (System.Windows.Forms.Application.OpenForms["Form1"] as Form1).DataReset();
                    }

Thank you everyone for helping :)