title: Numeric formatting of a referenced property in a DataGridView bound to a DataTable
body: <p>Further to <a href="https://stackoverflow.com/questions/59595313/responding-to-object-property-changes-within-a-class-object-hierarchy">this question</a> I am trying to implement a DataTable to use as a data source for a DataGridView. The original problem I am trying to get around is that the DGV presents a small subset of numbers from a sizeable object hierarchy and must reflect changes made to those objects. I cannot simply bind the DGV to the source data. I have therefore created an intermediate DataTable, populated it with the data to be displayed in the UI, and then bound that to the DGV.</p>
  
  <p>The problem I am facing now is that although all of the dynamic values are numeric, I cannot apparently set the display format for those cells using the DataGridViewCellStyle's Format property. If I try to set numbers to three decimal places using "######0.000" for instance, they are unchanged. This doesn't particularly surprise me since the DataTable's data type is 'object'. I set this intentionally for two reasons: 1) It must hold references to the underlying data, not values, and 2) the contents are a mixture of text, which remains fixed, and numbers, which will change. Therefore I think the DGV is not treating the values as numeric.</p>
  
  <p>Binding is done like this:</p>
  
  <pre><code>private void Test_Click(object sender, EventArgs e)
   {
       DataTable dt = populateDataTable();
       BindingSource SBind = new BindingSource();
       SBind.DataSource = dt;
       dataGridView1.DataSource = SBind;
       FormatDGV(dataGridView1);
  }
  </code></pre>
  
  <p>DataTable is populated like this:</p>
  
  <pre><code>private DataTable populateDataTable(int cols, int rows)
      {
  
          DataTable custTable = new DataTable("UITable");
          for (int i = 0; i &lt; cols; i++)
          {
              custTable.Columns.Add("", typeof(object));
          }
          for (int j = 0; j &lt; rows; j++)
          {
              custTable.Rows.Add(new object[cols]);
          }
  </code></pre>
  
  <p>... then add specific info which needs to be displayed in the UI. Text is added as object:</p>
  
  <pre><code>custTable.Rows[1][0] = (object)"foo";
  custTable.Rows[4][0] = (object)"bar";
  </code></pre>
  
  <p>I then try to set styles for the cells as I did previously (pre-binding):</p>
  
  <p><code>dgv[2 + i, 10].Style = greenText;</code></p>
  
  <p><code>DataGridViewCellStyle greenText = NewNumberFont(Color.Green, "######0.000");</code></p>
  
  <pre><code>private DataGridViewCellStyle NewNumberFont(Color fontColor, string numberFormat)
  {
      DataGridViewCellStyle numberFont = new DataGridViewCellStyle(dataGridView1.DefaultCellStyle);
      numberFont.Alignment = DataGridViewContentAlignment.MiddleRight;
      Font newFont = new Font(dataGridView1.Font.Name, 15, FontStyle.Regular);
      numberFont.Font = newFont;
      numberFont.ForeColor = fontColor;
      numberFont.Format = numberFormat;
      return numberFont;
  }
  </code></pre>
  
  <p>Is it possible to use this approach of an intermediate data source containing a mix of text and numbers, and still retain control over the number format that is displayed in the DataGridView?</p>