title: I'm trying to write a Value choosen from a Datetimepicker into a Textbox. However the Datetimepicker is null when i try to do so
body: <p>I have a DataGridView where I've inserted a Datetimepicker into a DataGridview column to pick the date.</p>
  <p>After I leave that Datetimepicker, I want to show the value I picked in the DataGridView column and I want to hide the Datetimepicker.</p>
  <p>For that I've added 2 events:</p>
  <pre><code>private void dgv_tableview_CellClick(object sender, DataGridViewCellEventArgs e)
  {
          try
          {
              switch (dgv_tableview.Columns[e.ColumnIndex].DataPropertyName)
              {
                  case &quot;Date&quot;:
                      // initialize DateTimePicker
                      DateTimePicker dtp = new DateTimePicker();
                      dtp.Format = DateTimePickerFormat.Short;
                      dtp.Visible = true;
                      dtp.Value = DateTime.Today;
  
                      // set size and location
                      var rect = dgv_tableview.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                      dtp.Size = new Size(rect.Width, rect.Height);
                      dtp.Location = new Point(rect.X, rect.Y);
  
                      // attach events
                      dtp.CloseUp += new EventHandler(dtp_CloseUp);
                      dtp.TextChanged += new EventHandler(dtp_OnTextChange);
                      dgv_tableview.Controls.Add(dtp);
                      break;
  
                  default:
                      break;
              }
          }
          catch(Exception)
          {
              MessageBox.Show(&quot;Bitte in ein Feld klicken&quot;);
          }            
  }
  
  // on picking Date, change Cell Value
  private void dtp_OnTextChange(object? sender, EventArgs? e)
  {            
          try
          {
              if (dtp != null)
                  dgv_tableview.CurrentCell.Value = dtp.Value.ToShortDateString();
          }
          catch(Exception)
          {
              MessageBox.Show(&quot;Fehler&quot;);
          }
  }
  
  // on leaving the cell, hide dtp
  void dtp_CloseUp(object? sender, EventArgs? e)
  {
          try
          {
              if (dtp != null)
                  dtp.Visible = false;
          }
          catch(Exception)
          {
              MessageBox.Show(&quot;Fehler&quot;);
          }
  }
  </code></pre>
  <p>Now in the functions that put the chosen date into the column and hide the datetimepicker, the datetimepicker (<code>dtp</code>) is null.</p>
  <p>However in debugging the <code>dgv_tableview_CellClick</code> function, the datetimepicker never turns null.</p>
  <p>I can't figure out why <code>dtp</code> is null in the event functions I've shown here.</p>