title: Can't get minimum datagridview width after changing font
body: <p>This question hasn't been asked elsewhere here as it's particular to having changed font. And other answers have a solution that doesn't work after a font change.</p>
  
  <p>The fix width button is meant to set the width of the datagridview so there are no scroll bars.</p>
  
  <p>If I click  add row, then fix width then change font then fix width, then fix width doesn't work.  So to label them  A,B,C,D</p>
  
  <p>A)add row         &lt;-- WORKS<br>
  B)fix width  &lt;---  WORKS<br>
  C)change font   &lt;--- WORKS<br>
  D)fix width,  &lt;--- FAILS  </p>
  
  <p>Fix width worked on line B, but fails on line D i.e. fails after the font change.</p>
  
  <p>I have an alternative method to get the min width, that i've included, but neither of them are working.. So the width isn't getting set correctly.</p>
  
  <p><a href="https://i.stack.imgur.com/2hznz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2hznz.png" alt="enter image description here"></a></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace dgvblah1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
  
              dataGridView1.AllowUserToAddRows = false; // maybe doesn't make a difference here for some reaosn. 
  
              dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
              dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  
              dataGridView1.Columns.Add("Col1", "Col1");
                  dataGridView1.Rows.Add(1);
  
              /*
              btnaddrow.PerformClick();
              btnchangefont.PerformClick();
              btnaddrow.PerformClick();
              btnfixwidth.PerformClick(); //fail
              */
  
          }
  
          private int seeifworks_get_min_dgv_width_A(DataGridView dgv)
          {
              // it's not too bad, it sometimes makes it the correct width and sometimes not quite enough, and so it's worth then running the next method that increments the width until there are no more horizontal scroll bars.
  
              int padding = 2; // clearly wrong here, it seems
              int tw = dgv.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dgv.RowHeadersWidth + padding + 2;
              int th = dgv.Rows.GetRowsHeight(DataGridViewElementStates.None) + dgv.ColumnHeadersHeight;
  
              return  tw;
          }
  
          private int seeifworks_get_min_dgv_width_B(DataGridView dgv)
          {
               // http://stackoverflow.com/questions/6651487/programmatically-resize-datagridview-to-remove-scroll-bars
              // Add two pixels for the border for BorderStyles other than None.
              var controlBorderWidth = (dgv.BorderStyle == BorderStyle.None) ? 0 : 2;
  
              // Return the width of all columns plus the row header, and adjusted for the DGV's BorderStyle.
              return dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + dgv.RowHeadersWidth + controlBorderWidth;
  
          }
  
  
          private void btnfixwidth_Click(object sender, EventArgs e)
          {
            //  dataGridView1.Width = seeifworks_get_min_dgv_width_A(dataGridView1);
  
              dataGridView1.Width = seeifworks_get_min_dgv_width_B(dataGridView1);
  
  
          }
  
          private void btnaddrow_Click(object sender, EventArgs e)
          {
              dataGridView1.Rows.Add(1);
          }
  
          private void btnchangefont_Click(object sender, EventArgs e)
          {
              dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Courier New", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  
              for (int i = 0; i &lt; dataGridView1.Rows.Count; i++)
                  dataGridView1.Rows[i].DefaultCellStyle.Font = new Font("Courier New", 30, FontStyle.Bold);
  
              dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Courier New", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  
          }
      }
  }
  </code></pre>