【问题标题】:DataGridView.CellFormatting doesn't work when the grid is sorting网格排序时 DataGridView.CellFormatting 不起作用
【发布时间】:2011-08-24 17:51:28
【问题描述】:

我有一个 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 行为如此奇怪的原因或提供更好的方法,他们将得到公认的答案!

【问题讨论】:

  • 更新:我尝试使用 DataGridView.Paint 事件并循环遍历行。虽然这可行,但它会导致行首先以其原始颜色显示,然后更改为正确的颜色。 CellFormatting 事件可能是当时使用的正确事件,但出现上述问题...

标签: .net vb.net datagridview cell-formatting


【解决方案1】:

我建议您尝试RowPrePaint,因为它可以让您在表格绘制之前更改表格,但在表格绑定数据之后。

【讨论】:

  • RowPrePaint 产生了同样的问题。 e.RowIndex 始终为零。
  • @John “您可以单独处理此事件,也可以结合 RowPostPaint 事件处理此事件,以自定义控件中行的外观。” -msdn。如果它不能处理您想要的任何这些事件,我只是不确定
【解决方案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

结束子

【讨论】:

    【解决方案3】:

    您的问题是 DataGridView.CellFormatting 是一个单元格级别的事件,但您正在使用它来格式化整行。

    CellFormatting 会针对每个可见单元格触发,并且对于每个单元格,您都将重新格式化整行(通过CurrentRow.DefaultCellStyle),然后会为重新格式化的单元格触发更多单元格格式化事件。这可能会产生一个从内部转义的事件循环,但它会为您提供RowIndex 的虚假值。

    如果您将代码更改为仅重新设置相关单元格的样式,您的问题就会消失:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2023-03-27
      • 2012-02-15
      • 2017-10-11
      相关资源
      最近更新 更多