DataGridView cells
의 forecolor를 변경하는 데 문제가 있습니다. 나는 여러 런타임이 DataGridView
을 생성하고 난 다음 코드를 사용하고 있습니다Forecolor는 어떻게 변경합니까?
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 < 0 Then
dr.Cells(dc.Index).Style.ForeColor = Color.Red
ElseIf dr.Cells(dc.Index).Value > 0 Then
dr.Cells(dc.Index).Style.ForeColor = Color.Green
End If
End If
Next
End If
Next
dgv.Refresh()
End If
Next
Next
이 코드는 있지만 다른 DataGridview
에서, 만 현재 포커스가있는 DataGridView에 잘 작동합니다.
내 코드를 수정하고 지금은 다음과 같이 올라와있다 :
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 > 0 Then
e.CellStyle.ForeColor = Color.Green
ElseIf intCellValue < 0 Then
e.CellStyle.ForeColor = Color.Red
End If
End If
End If
End Sub
이 모두 잘 작동합니다. 도와 주셔서 감사합니다!
... 당신이 시도 할 수있는 작은 조각을했다,하지만 난 또 다른 문제가있다. 위의 코드를 수정했습니다. – J2Tuner
사실이 코드는 완벽하게 작동했습니다. 감사! – J2Tuner