title: C# DataGridView retrieving outdated results from database when Child Form is closed
body: <p>I have a form opens up a <strong>dialog</strong> that contains a DataGridView. </p>
  
  <p>When I double click the DataGridView, it opens up a child form that contains details of that row. </p>
  
  <p>Once I save any updates, the child form closes and the DataGridView should be refreshed.</p>
  
  <p>However, I have noticed that the DataGridView's refresh function is not getting the right data back from the database. It is getting the data from BEFORE the update and not AFTER the update thus displaying incorrect data.</p>
  
  <p>I am unsure of how to continue on from here as my current method is to refresh the DataGridView directly from the Child Form</p>
  
  <p>Here is my Code</p>
  
  <p>This is the double click action to open the child form</p>
  
  <pre><code>private void fieldsDataGridView_DoubleClick(object sender, EventArgs e)
  {
      foreach (DataGridViewRow row in fieldsDataGridView.SelectedRows)
      {
          fieldIdInput.Text = row.Cells["Id"].Value.ToString();
          DialogResult dr = new DialogResult();
          EditFields editFields = new EditFields(this,programIdInput.Text, fieldIdInput.Text, false);
          editFields.Show();
      }
  }
  </code></pre>
  
  <p>This is the Refresh function contained on the first form</p>
  
  <pre><code>public void RefreshDataGrid()
  {
      var selected = programInput.SelectedValue;
      if (selected != null)
      {
          var result = programsData.Get(selected.ToString());
          if (result != null)
          {
              programIdInput.Text = result.Id;
              LoadList(result.Id);
          }
      }
      if (selected == "-1")
      {
          RecursiveClearInputs(this.Controls);
          programIdInput.Text = "";
          fieldIdInput.Text = "";
          fieldsDataGridView.DataSource = null;
      }
      fieldsDataGridView.Refresh();
      fieldsDataGridView.ClearSelection();
  }
  
  private void LoadList(string Input)
  {
      fieldsDataGridView.DataSource = null;
      List&lt;Field&gt; fields = new List&lt;Field&gt;();
      fields = fieldsData.GetAllByTaskId(Input);
      List&lt;FieldsDGViewModel&gt; fdgvm = new List&lt;FieldsDGViewModel&gt;();
      foreach (var item in fields)
      {
          var f = new FieldsDGViewModel
          {
              Id = item.Id,
              Name = item.Name,
              Order = item.Order,
              IsPrint = item.IsPrint
          };
          fdgvm.Add(f);
      }
      fdgvm = fdgvm.OrderBy(x =&gt; x.Order).ToList();
      fieldsDataGridView.DataSource = fdgvm;
      fieldsDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      fieldsDataGridView.Columns["Id"].Visible = false;
      fieldsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  }
  </code></pre>
  
  <p>This is the child forms constructor</p>
  
  <pre><code>private string ProgramId;
  private string FieldId;
  private bool IsNew;
  private Fields Field;
  ProgramsData programsData = new ProgramsData();
  FieldsData fieldsData = new FieldsData();
  PrinterData printerData = new PrinterData();
  
  public EditFields(Fields Field, string ProgramId, string FieldId, bool IsNew)
  {
      this.ProgramId = ProgramId;
      this.FieldId = FieldId;
      this.IsNew = IsNew;
      this.Field = Field;
      InitializeComponent();
      this.FormClosing += new FormClosingEventHandler(this.EditFields_FormClosing);
  }
  </code></pre>
  
  <p>This is the Child Forms closing function</p>
  
  <pre><code>private void EditFields_FormClosing(object sender, FormClosingEventArgs e)
  {
      Field.RefreshDataGrid();
  }
  </code></pre>