2011-08-24 21 views
2

我有一個datagridview控件,我需要根據每行的一個單元格中的值爲行着色。我使用的是CellFormatting事件,像這樣:當網格排序時,DataGridView.CellFormatting不起作用

Private Sub DGDisplay_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgDisplay.CellFormatting 

    Dim intRowIndex As Integer = e.RowIndex 'This is zero when sorting..... 
    Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex) 
    Dim strTestValue As String = CurrentRow.Cells("Status").Value 

    Select Case UCase(strTestValue) 
     Case "WARNING" 
      CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff 
     Case "ERRMESSAGE" 
      CurrentRow.DefaultCellStyle.BackColor = Color.Salmon 
    End Select 
End Sub 

此工作正常時,電網負荷,當我把它的滾動,等等。但是當我在列標題點擊排序的網格,e.RowIndex總是零和所有的行得到第一行的格式...

爲什麼這不工作時,網格排序?

編輯: 的Joakim是在正確的軌道上,但下面的代碼工作正常:

Private Sub dgDisplay_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgDisplay.CellPainting 

    If e.RowIndex < 0 Then 
     Exit Sub 
    End If 

    Dim intRowIndex As Integer = e.RowIndex 
    Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex) 
    Dim strTestValue As String = CurrentRow.Cells("Status").Value 

    Select Case UCase(strTestValue) 
     Case "WARNING" 
      CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff 
     Case "ERRMESSAGE" 
      CurrentRow.DefaultCellStyle.BackColor = Color.Salmon 
    End Select 
End Sub 

出於某種原因,e.RowIndex設置正確,在這裏,但沒有其他方法。你唯一需要擔心的是它可以是-1。但是當我嘗試使用其他方法時,包括PrePaint,我必須處理它總是在某種程度上變爲零。如果我排除零情況,就像我排除了負面情況一樣,那麼第一行總是白色!瘋狂......我不確定這是爲什麼起作用,但它確實如此。它也不會產生超出我使用CellFormatting事件的閃爍。

如果任何人都能解釋這個原因爲什麼e.RowIndex正在做這麼一個偉大的或提供一個更好的方式來做到這一點,他們會得到答案!

+0

更新:我嘗試使用DataGridView.Paint事件,並通過循環行。雖然這可行,但會導致行首先以其原始顏色顯示,然後更改爲適當的顏色。 CellFormatting事件可能是正確的使用然後,但上述問題發生... – John

回答

2
Private Sub dgDisplay_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgDisplay.CellPainting 

If e.RowIndex < 0 Then 
    Exit Sub 
End If 

Dim intRowIndex As Integer = e.RowIndex 
Dim CurrentRow As DataGridViewRow = dgDisplay.Rows(intRowIndex) 
Dim strTestValue As String = CurrentRow.Cells("Status").Value 

Select Case UCase(strTestValue) 
    Case "WARNING" 
     CurrentRow.DefaultCellStyle.BackColor = Color.PeachPuff 
    Case "ERRMESSAGE" 
     CurrentRow.DefaultCellStyle.BackColor = Color.Salmon 
End Select 

末次

2

我建議你試試RowPrePaint,因爲它可以讓你在繪製任何東西之前修改表格,但是在它有數據綁定它之後。

+0

RowPrePaint產生相同的問題。 e.RowIndex始終爲零。 – John

+0

@John「您可以單獨處理此事件或與RowPostPaint事件組合來自定義控件中行的外觀。」 -msdn。如果它不像你想要的那樣與那些事件一起工作,我只是不確定 – Joakim

0

您的問題是DataGridView.CellFormatting是細胞級別的事件,但你用它來全行格式。

CellFormatting對每個可見單元格觸發,對於每個單元格,您將重新格式化整行(通過CurrentRow.DefaultCellStyle),然後爲重新格式化的單元格觸發更多的單元格格式化事件。這可能會產生一個從內部轉義出來的事件循環,但它會給你一個僞造值RowIndex

如果你改變你的代碼只restyle相關的電池,您的問題就會消失:

Dim currentCell As DataGridViewCell = CurrentRow(e.ColumnIndex) 

其次:

Select Case UCase(strTestValue) 
    Case "WARNING" 
     currentCell.Style.BackColor = Color.PeachPuff 
    Case "ERRMESSAGE" 
     currentCell.Style.BackColor = Color.Salmon 
End Select