title: How to replace the HeaderCells of a DataGridView with custom headers?
body: <p>When to replace a <code>DataGridViewColumnHeaderCell</code> with a custom <code>HeaderCell</code> (here: <code>CustomDataGridViewColumnHeaderCell</code>), when the <code>DataSource</code> of a DataGridView is already set to a <code>DataTable</code> object and also preserve the text of the previous HeaderCell, which is set to the name of the corresponding Column of the DataTable?</p>
  <p>I have a custom header class for a <code>DataGridView</code>:</p>
  <pre><code>public class CustomDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell
  {
      // adds a button to the header
  }
  </code></pre>
  <p>A <code>DataTable</code> is being set as the <code>DataSource</code>, the Columns are created automatically. How can I replace the default header cells with custom header cells (instances of the above)?</p>
  <p><strong>More detail</strong></p>
  <p><a href="https://i.stack.imgur.com/ChHbo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ChHbo.png" alt="enter image description here" /></a></p>
  <p>The problem to solve is, a <code>DataGridView</code> needs to have clickable buttons embedded in its header cells, where the arrow is pointing to in the screenshot above. For that, there is a class <code>CustomDataGridViewColumnHeaderCell</code>. To show data in the <code>DataGridView</code>, a <code>DataTable</code> is being assigned as its data source. For example:</p>
  <pre><code>myDataGridView.DataSource = myDataTable;
  </code></pre>
  <p>After doing this, the <code>DataGridView</code> will have columns corresponding to those in the <code>DataTable</code>. I have derived a class from the default header classes, so that the buttons appear inside the column headers.</p>