Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you change the Forecolor?
    primarykey
    data
    text
    <p>I am having trouble changing the forecolor of <code>DataGridView cells</code>. I have multiple <em>runtime</em> generated <code>DataGridView</code> and I am using the following code:</p> <pre><code>For Each tbp As TabPage In frmTimingP2P.tabctrlTimingTable.TabPages For Each ctrl As Control In tbp.Controls Dim dgv As DataGridView = TryCast(ctrl, DataGridView) If Not dgv Is Nothing Then For Each dc As DataGridViewColumn In dgv.Columns If dc.Name.EndsWith("Gain/Loss") Then For Each dr As DataGridViewRow In dgv.Rows If Not dr.Cells(dc.Index).Value Is DBNull.Value Then If dr.Cells(dc.Index).Value = 0 Then dr.Cells(dc.Index).Style.ForeColor = Color.Blue ElseIf dr.Cells(dc.Index).Value &lt; 0 Then dr.Cells(dc.Index).Style.ForeColor = Color.Red ElseIf dr.Cells(dc.Index).Value &gt; 0 Then dr.Cells(dc.Index).Style.ForeColor = Color.Green End If End If Next End If Next dgv.Refresh() End If Next Next </code></pre> <p>This code works fine ONLY on the currently focused DataGridView, but not in others <code>DataGridview</code>.</p> <p>I have revised my code and have now come up with the following:</p> <pre><code>Private Sub dgvOverview_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvOverview.CellFormatting Dim dgv As New DataGridView dgv = sender If dgv.Columns(e.ColumnIndex).Name.EndsWith("Gain/Loss") Then If e.Value IsNot DBNull.Value Then Dim intCellValue As Integer = CType(e.Value, Integer) If intCellValue = 0 Then e.CellStyle.ForeColor = Color.Blue ElseIf intCellValue &gt; 0 Then e.CellStyle.ForeColor = Color.Green ElseIf intCellValue &lt; 0 Then e.CellStyle.ForeColor = Color.Red End If End If End If End Sub </code></pre> <p>This all works well. Thanks for the help!</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.
 

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