title: How to access a cell in a DataRowView based on the columns DataPropertyName?
body: <p>I have a Windows Forms application with a <code>DataSet</code>. I've simply used the Data | Add New DataSource to add the Products table of the Northwind database to my DataSources and created a <code>DataGridView</code> showing the contents of the Products table. I just dragged the Products table from the Data Sources window to the form, so all the columns are created automatically.</p>
  
  <p>Now, I want the rows containing a product where the Discontinued column is true to be painted in a different color. I've created a <code>CellPainting</code> event handler for it, but I'm having trouble locating the value for the Discontinued column.</p>
  
  <p>Because the <code>DataGridView</code> is created automatically, the Columns in it have names like <code>dataGridViewTextBoxColumn1</code>, which has a <code>DataPropertyName</code> of "ProductID".</p>
  
  <p>My question is: how can I  find the value for Discontinued based on the <code>DataPropertyName</code>? Or am I required to use the name of the column itself? <em>(In which case I better give it a meaningful name)</em></p>
  
  <p>My code is:</p>
  
  <pre><code>private void productsDataGridView_CellPainting(object sender,
      DataGridViewCellPaintingEventArgs e)
  {
      if (e.RowIndex &gt;= 0)
      {
          var row = productsDataGridView.Rows[e.RowIndex];
          if ((bool) (row.Cells[ nameOrIndexOfColumn ].Value))
          {
              e.CellStyle.ForeColor = Color.DarkGray;
          }
      }
  }
  </code></pre>
  
  <p>How can I access the Value of the Column with <code>DataPropertyName</code> "Discontinued"?</p>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <p>Based on Neil Barnwell's answer, this seems to be a way.</p>
  
  <pre><code>private void productsDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {
      if (e.RowIndex &gt;= 0)
      {
          var productView = (DataRowView) productsDataGridView.Rows[e.RowIndex].DataBoundItem;
          var product = productView.Row as NorthwindDataSet.ProductsRow;
          if (product != null &amp;&amp; product.Discontinued)
          {
              e.CellStyle.ForeColor = Color.DarkGray;
          }
      }
  }
  </code></pre>
  
  <p>The big advantage of this is that the Discontinued value doesn't have to be an actual column on the DataGridView.</p>