title: DataGridView - get actual column widths
body: <p>I am developing an ASP.NET website in which I have some tables of data. I want to generate images of this data, so that I can display those images on forums (for example). The method I have used in the past is quite simple: I create a System.Windows.Forms.DataGridView control (yes, winforms control!) in memory, style it, load some columns and rows, and then use its DrawToBitmap method to generate an image (basically a "screenshot" of the control).</p>
  
  <p>Example:</p>
  
  <pre><code>    private static Image WriteGrid(List&lt;object[]&gt; data)
      {
          var grid = new DataGridView();
          grid.Columns.Add("Column 1", "Column 1");  
          grid.Columns.Add("Column 2", "Column 2"); 
          grid.Columns.Add("Column 3", "Column 3");        
  
          // Ensure columns are large enough to display all data
          grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  
          foreach (var row in data)
              grid.Rows.Add(row);         
  
          var width = GetWidth(grid);
          var height = GetHeight(grid);
  
          grid.Width = width;
          grid.Height = height;
  
          var bmp = new Bitmap(width, height);
          grid.DrawToBitmap(bmp, grid.Bounds);
          bmp.Save(Util.Server.MapPath("~/Images/grid.png"), ImageFormat.Png);
          return bmp;
      }
  </code></pre>
  
  <p>This works really well, however I am having trouble calculating the size of the image I should generate, as well as the size of the DataGridView control itself. By default, it is 100x100 (I think) which therefore cuts out most of the data, which is useless for image generation since you can't scroll the image obviously...</p>
  
  <p>So I want to calculate the width and height that the DataGridView should be so that it can display all data without scrollbars. </p>
  
  <p>I thought this would be pretty simple: loop through the columns and add up their Width property to get the total width, then loop through the rows and add their Height property to get the total height:</p>
  
  <pre><code>    private static int GetWidth(DataGridView grid)
      {
          int width = 0;
          foreach (DataGridViewColumn col in grid.Columns)
          {
              width += col.Width;
          }
          return width;
      }
  
      private static int GetHeight(DataGridView grid)
      {
          int height = grid.ColumnHeadersHeight;
          foreach (DataGridViewRow row in grid.Rows)
          {
              height += row.Height;
          }
          return height;
      }
  </code></pre>
  
  <p>Here's where the trouble comes in though... The Width property of each column always returns 100, which I am assuming is the (default) MinimumWidth of the column rather than the actual width of the column. Therefore with three columns my grid is always 300 pixels wide, even if the columns are actually much wider than 100 pixels each.</p>
  
  <p>I am guessing it's caused by the fact that I'm creating the grid in memory rather than actually drawing it to some Form? I believe this should work, but for some reason whatever I do the column widths are always 100... This makes my entire idea useless because I have no way to calculate the necessary width of the grid and image. I suppose I could measure all the strings of data in each column and basically resize them manually, but I'd really rather not... There must be a better way?</p>
  
  <p>What am I doing wrong?
  Thanks!</p>