2013-01-11 3 views
1

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 

이 모두 잘 작동합니다. 도와 주셔서 감사합니다!

답변

0

나는이 나를 위해 일했다

For i As Integer = 0 To Me.dataGridView1.RowCount - 1 
If Me.dataGridView(3, i).Value = 0 Then 
    Me.dataGridView(4, i).Style.BackColor = Color.Blue 
End If 
Next 
+0

이 코드에는 운이 없었습니다. 그것은 내가 가지고있는 것과 거의 똑같은 일을했습니다. 그래도 고마워. – J2Tuner

관련 문제