Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The correct way to do this with the DataGridView is to use the CellFormating event, described <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx" rel="nofollow">here</a> on MSDN. There is a nice example of what you can do in a <a href="http://msdn.microsoft.com/en-us/library/z1cc356h.aspx" rel="nofollow">HowTo Article</a>, but I've also given an overview here.</p> <p>First you need to subscribe to the <code>CellFormating</code> event, either through the design view or through code:</p> <pre><code>this.dataGridView1.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView1_CellFormatting); </code></pre> <p>Then in the event handler you place your logic to format your data for display:</p> <pre><code>private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (!dataGridView1.Rows[e.RowIndex].IsNewRow &amp;&amp; e.ColumnIndex == dataGridView1.Columns["InterestingColumn"].Index) { if (e.Value == null) return; if (e.Value.ToString() == "TargetValue") { e.Value = "DisplayValue"; } } } </code></pre> <p>This does not alter the underlying value in the DataSet - only the displayed value, and the event triggers after each cell leave event, so users can type data into the grid (if it is not readonly) and the formatting will be applied.</p> <p>Of course all this is customisable as you wish, but the CellFormating event is the best place to start.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload