2016-09-09 14 views
1

값에 따라 DataGridView 행을 강조 표시하려고합니다. 값이 유효성을 확인한 다음 녹색이면 빨간색으로 표시됩니다.
셀이 이전에 포맷되지 않았 으면 녹색과 빨간색이 나타납니다. 그러나 내가 녹색 행으로 돌아가서 검증되지 않은 값을 변경하면 한 셀이 빨간색으로 바뀌지 않습니다. 그것은 녹색으로 유지됩니다. 단지 첫 번째 셀은 빨간색으로 어떤 항목
enter image description here
2) 체결
enter image description here
3) 다시 가서 당신이 볼 수 있듯이 잘못된 값
enter image description here
를 입력 검증 된 값없이
1) :
은 사진보기 ,하지만 나는 전체 행이 빨갛게되기를 바란다.
CellValidating 이벤트에서 셀의 서식을 지정 중입니다. 코드는 다음과 같습니다.
DataGridView 행 강조 표시

Private Sub dgvItems_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvItems.CellValidating 
    If e.ColumnIndex = ValueColumnIndex Then 
     If Not ValidateValue(e.FormattedValue) Then 
      Me.dgvItems.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightPink 
      Me.dgvItems.DefaultCellStyle.SelectionBackColor = Color.LightPink 
      Me.dgvItems.DefaultCellStyle.SelectionForeColor = Color.Black 
      e.Cancel = True 
     Else 
      Me.dgvItems.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight 
      Me.dgvItems.DefaultCellStyle.SelectionForeColor = SystemColors.HighlightText 
      Me.dgvItems.Rows(rowIndex).DefaultCellStyle.BackColor = Color.LightGreen 
      Me.dgvItems.Rows(rowIndex).DefaultCellStyle.ForeColor = Color.Black 
     End If 
    End If 
End Sub 

도움을 주시면 감사하겠습니다. 감사.

답변

1

DataGridView의 속성에서 아래 이미지와 같이 포커스> RowValidating을 찾습니다. 이 같은

enter image description here

무언가가 당신을 위해 그것을 할 것이다.

Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) 

    Dim drv As DataRowView 
    Dim c As Color 
    If drv.Item("Gender").ToString = "M" Then 
     c = Color.LightBlue 
    Else 
     c = Color.Pink 
    End If 
    e.CellStyle.BackColor = c 

End Sub 
0

전체 행의 형식을 다시 지정하려면 RowValidating 이벤트 처리기를 사용하십시오. 유효성 검증에 단일 셀 스타일을 다시 지정하려면

후 사용 CellValidating

라인 :

Me.dgvItems.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight 

등등 이러한 변경 사항을 설정 전체 그리드의 기본 셀 스타일에 글로벌 효과가 Load을 입력 한 다음 이벤트 핸들러에서 조건부 서식을 지정하십시오.