vb.net DataGridView 当前行变化

  Private Sub dataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
        If e.RowIndex >= DataGridView1.Rows.Count - 1 Then
            Return
        End If
        Dim oldForeColor As New Color()
        Dim oldBackColor As New Color()
        'object是所有类的基类为引用类型
        Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        If row Is DataGridView1.CurrentRow Then
            '判断是否是操作过的行
            If row.DefaultCellStyle.ForeColor <> Color.White Then
                oldForeColor = row.DefaultCellStyle.ForeColor
                row.DefaultCellStyle.ForeColor = Color.White
            End If
            If row.DefaultCellStyle.BackColor <> Color.Blue Then
                oldBackColor = row.DefaultCellStyle.BackColor
                row.DefaultCellStyle.BackColor = Color.Blue
            End If
        Else
            row.DefaultCellStyle.ForeColor = oldForeColor
            row.DefaultCellStyle.BackColor = oldBackColor
        End If
    End Sub

猜你喜欢

转载自blog.csdn.net/laocooon/article/details/120017394