I have a form with a DataGridView and I want to set the columns AutoSizeMode to Fill and the grids ColumnHeadersHeightSizeMode
This seems to be a bug - the code is trying to access dataGridView.TopLeftHeaderCell, which when happens for the first time actually creates that cell and triggers some layout actions not expected at that moment.
With all that in mind, the fix is simple. We need to make sure that the TopLeftHeaderCell is created before DataGridView handle, by adding the following line (before affffding the grid to Controls for instance)
var topLeftHeaderCell = grid.TopLeftHeaderCell; // Make sure TopLeftHeaderCell is created