title: Format DataGridView Cell without changing underlying value?
body: <p>I have an unbound DataGridView (in VS 2008), of which one column contains a file path. I'd like to format the string using the TextRenderer class on the ColumnWidthChanged event without actually modifying the underlying value. The problem is that the contents of the table are saved when the form is closed and I don't want to save the formatted value. I think I'm just in too deep to see the obvious solution so I'm depending on you guys to point it out :-).</p>
  
  <p>The idea is to display this:</p>
  
  <p>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe</p>
  
  <p>...as this (depending on the width of the column):</p>
  
  <p>C:\Program Files\Microso…\gacutil.exe</p>
  
  <hr>
  
  <p>It appears I spoke too soon. I'm getting some very strange results from TextRenderer.MeasureText(). If I hardcode the path value as "C:\Documents and Settings\jluce\My Documents\Downloads" it comes ends up as 
  C:\Documents and Settings\jluce\M...\Downloads\0wnloads". If I don't hardcode it (like below) it gets further corrupted each time I resize the column.</p>
  
  <p>Here's what it looks like after a couple resizes:
  <a href="http://i33.tinypic.com/rh0rj6.gif" rel="nofollow noreferrer">Screenshot</a></p>
  
  <p>Here's what I'm currently doing.</p>
  
  <pre><code>  if (e.ColumnIndex == 1)
    {
      foreach (DataGridViewRow Row in mappingsDataGrid.Rows)
      {
        string Path = (string)Row.Cells[1].Value;
        Path = Path.Trim();
  
        TextRenderer.MeasureText(Path, e.CellStyle.Font,
          new Size(mappingsDataGrid.Columns[e.ColumnIndex].Width, Row.Height),
            TextFormatFlags.ModifyString | TextFormatFlags.PathEllipsis);
  
        e.Value = Path;
      }
    }
  </code></pre>
  
  <hr>
  
  <p>This just keeps getting weirder!!</p>
  
  <p>I managed to fix the problem of the mangled string by iterating through each char and removing the bad ones. However, now I've got an even crazier problem. A local variable I'm assigning in the event handler is retaining its value between calls.</p>
  
  <p>Here's the relevant code:</p>
  
  <pre><code>     string Path = ""; // &lt;-- #1
       Path = "C:\\Documents and Settings\\jluce\\My Documents\\Downloads"; // &lt;-- #2
  
        TextRenderer.MeasureText(Path, Row.Cells[1].Style.Font,
          new Size((mappingsDataGrid.Columns[e.Column.Index].Width), Row.Height),
            TextFormatFlags.ModifyString | TextFormatFlags.PathEllipsis);
  
        // Left out code that strips invalid chars
  
        Row.Cells[1].Value = Path; // &lt;-- #3
        Path = null;
  </code></pre>
  
  <p>First time resizing column (refer to #'s in the comments above):</p>
  
  <ol>
  <li>After this line Path contains "".</li>
  <li>After this line Path contains string just as it appears above. </li>
  <li>Path contains truncated file path as it should (i.e. "C:\Documents and Setti...\Downloads")</li>
  </ol>
  
  <p>Second time resizing:</p>
  
  <ol>
  <li>After this line Path contains "", as it should. </li>
  <li>After this line Path contains "C:\Documents and Set...\Downloads\0 Documents\Downloads", which was the invalid value from the previous iteration before I stripped out the invalid characters (seen here as '\0')!!</li>
  <li>Now the path is FUBAR because I started with a screwed up string and it just keeps getting worse.</li>
  </ol>
  
  <p>Why would Path be assigned the invalid value from the previous function call (after correctly assigning an empty string!) when I'm explicitly assigning a value to it?!!!!!</p>