title: How do you draw a border around a DataGridView cell while it's being edited?
body: <p>I would like to draw a red border around a <code>DataGridView</code> cell while it's being edited.</p>
  
  <p>I've managed to draw a red border around the selected cell while it's not being edited using this code:</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
  {
      this.Width = 650;
      this.Height = 250;
      dataGridView1.Left = 5;
      dataGridView1.Top = 5;
      dataGridView1.Width = 600;
      dataGridView1.Height = 175;
  
      DataTable dt = new DataTable("Test Table");
      dt.Columns.Add("Column 1");
      dt.Columns.Add("Column 2");
      dt.Columns.Add("Column 3");
      dt.Columns.Add("Column 4");
      dt.Columns.Add("Column 5");
      dt.Rows.Add(dt.NewRow());
      dt.Rows.Add(dt.NewRow());
      dt.Rows.Add(dt.NewRow());
      dt.Rows.Add(dt.NewRow());
      dt.Rows.Add(dt.NewRow());
      dataGridView1.DataSource = dt;
  
      dataGridView1.AllowUserToAddRows = false;
      dataGridView1.AllowUserToDeleteRows = false;
      dataGridView1.MultiSelect = false;
      dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
      dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
      dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
      dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
  }
  
  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {
      if (e.ColumnIndex != -1 &amp;&amp; e.RowIndex != -1 &amp;&amp; dataGridView1[e.ColumnIndex, e.RowIndex].Selected)
      {
          using (Brush borderBrush = new SolidBrush(Color.Red))
          {
              using (Pen borderPen = new Pen(borderBrush, 2))
              {
                  Rectangle rectDimensions = e.CellBounds;
                  rectDimensions.Width -= 2;
                  rectDimensions.Height -= 2;
                  rectDimensions.X = rectDimensions.Left + 1;
                  rectDimensions.Y = rectDimensions.Top + 1;
  
                  e.Graphics.DrawRectangle(borderPen, rectDimensions);
  
                  e.Handled = true;
              }
          }
      }
  }
  </code></pre>
  
  <p>Which produces this result:</p>
  
  <p><a href="https://i.stack.imgur.com/EeCWg.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/EeCWg.jpg" alt="Image1"></a></p>
  
  <p>However, when you edit a cell this happens:</p>
  
  <p><a href="https://i.stack.imgur.com/C0Vmf.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/C0Vmf.jpg" alt="Image2"></a></p>
  
  <p>It seems the <code>EditingControl</code> is drawing itself over the top of most of my red border.  Unfortunately, I can't find a way to fix this so my red border will stay fully displayed at all times.  </p>
  
  <p>How can I do this???</p>
  
  <hr>
  
  <p><br>
  <strong>Here's what I've tried so far:</strong></p>
  
  <p><strong>1.</strong> Handling the <code>EditingControlShowing()</code> event to manually re-draw the
      border like this:</p>
  
  <pre><code>private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  {
      Graphics gfx = e.Control.CreateGraphics();
  
      using (Brush borderBrush = new SolidBrush(Color.Red))
      {
          using (Pen borderPen = new Pen(borderBrush, 2))
          {
              Rectangle rectDimensions = e.Control.ClientRectangle;
              rectDimensions.Width -= 2;
              rectDimensions.Height -= 2;
              rectDimensions.X = rectDimensions.Left + 1;
              rectDimensions.Y = rectDimensions.Top + 1;
  
              gfx.DrawRectangle(borderPen, rectDimensions);
          }
      }
  }
  </code></pre>
  
  <p>But this didn't draw anything.  I tried a few variations of this but all of them still drew nothing here.  </p>
  
  <p><br>
  <strong>2.</strong> I then tried to handle the <code>Paint()</code> event of the <code>EditingControl</code>
      like this:</p>
  
  <pre><code>private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  {
      e.Control.Paint -= new PaintEventHandler(dataGridView1_EditingControl_Paint);
      e.Control.Paint += new PaintEventHandler(dataGridView1_EditingControl_Paint);
  }
  
  void dataGridView1_EditingControl_Paint(object sender, PaintEventArgs e)
  {
      MessageBox.Show("Starting EditingControl Paint() Event...");
  }
  </code></pre>
  
  <p>But this event doesn't even fire.  I later read somewhere that the
      <code>EditingControl</code> uses a normal <code>TextBox</code>, which doesn't fire the
      <code>Paint()</code> event because it's handled by Windows instead.</p>
  
  <p><br>
  <strong>3.</strong> Finally, rather than trying to re-paint another border, I decided to
      try and hack around it by resizing the <code>EditingControl</code> to be
      smaller than my border in hopes the border would then show around
      it, like this:</p>
  
  <pre><code>private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  {
      e.Control.Resize -= new EventHandler(dataGridView1_EditingControl_Resize);
      e.Control.Resize += new EventHandler(dataGridView1_EditingControl_Resize);
  }
  
  void dataGridView1_EditingControl_Resize(object sender, EventArgs e)
  {
      dataGridView1.EditingControl.Left = 20;
  }
  </code></pre>
  
  <p>However, that just gave me this result:</p>
  
  <p><a href="https://i.stack.imgur.com/wL1bH.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/wL1bH.jpg" alt="Image3"></a></p>
  
  <p>So the <code>TextBox</code> did move over to the Left, but it seems there is another
      control underneath it that is still blocking my red border.  However, I can't find anyway to get access
      to that control to resize it so this didn't work for me either.  </p>
  
  <p><br>
  <strong>4.</strong> I also tried using the code from #1 above to re-draw the border in the <code>Resize()</code> event, but that still did nothing.  Although, using <code>dataGridView1.EditingControl.BackColor = Color.Red;</code> did work so I can format some parts of the control here, but it seems trying to draw a border isn't one of them.</p>
  
  <p>All I want to do is keep a red border showing around the cell while it's being edited.  Do you know how I can do this?</p>