경제적 인 방법은 CellFormatting
이벤트를 사용하는 것입니다. 이 셀이 처음으로 표시되면 실행됩니다. 보이는 행에 대해서만 실행되므로 더 빨라야합니다.
Then I loop through the grid view to do something like update the data of all colored rows
데이터를 DB에서 오는 경우
는 데이터 소스와 같은 데이터에 작용되어야한다 (영업 쿼리를 언급). DGV는 사용자의 데이터보기입니다. 특정 색상의 행에 대해 조치를 취하는 대신 파트 번호 나 색상을 결정하는 요소에 대해 조치를 취하십시오. 또한이 번호 또는 부품 번호가있는 레코드 뷰를 작성하고 프로세스/프로세스를 단순화 할 수 있습니다. 데이터를 변경할 수 있기 때문에
, 당신은이 이벤트를 처리 할 수 있습니다 : CellFormatting
및
Private Sub dgv1_CellFormatting(...) Handles dgv1.CellFormatting
' default start up color
If e.ColumnIndex = 3 Then
e.FormattingApplied = ColorMyRow(e.RowIndex, e.ColumnIndex)
Else
e.FormattingApplied = False
End If
End Sub
Private Sub dgv1_CellValueChanged(...) Handles dgv1.CellValueChanged
' if the target cell changes, update
If e.ColumnIndex = 3 Then
ColorMyRow(e.ColumnIndex, e.RowIndex)
End If
End Sub
' DRY
Private Function ColorMyRow(rowIndex As Int32, colIndex As Int32) As Boolean
Dim bass As Color = Color.PeachPuff
Dim pike As Color = Color.SeaShell
Dim salmon As Color = Color.Salmon
Select Case dgv1.Rows(rowIndex).Cells(colIndex).Value.ToString
Case "Bass"
dgv1.Rows(rowIndex).DefaultCellStyle.BackColor = bass
Return True
Case "Pike"
dgv1.Rows(rowIndex).DefaultCellStyle.BackColor = pike
Return True
Case "Salmon"
dgv1.Rows(rowIndex).DefaultCellStyle.BackColor = salmon
Return True
End Select
Return False
End Function
색상은) 사용자가 셀을 편집 여부를 업데이트됩니다
CellValueChanged
, B)는 셀 값을 변경 (dgv1.Rows(0).Cells(3).Value = "Mermaid"
) 또는 c) 데이터 소스 (dtParts.Rows(0)(3) = "Pike"
)를 변경합니다.
Dim bassRows = dtSample.Select("Fish = 'Bass'")
For Each dr As DataRow In bassRows
dr("Fish") = "Pike"
Next
색상이 자동적으로 변경됩니다 마지막으로
,보다는 파란색 행에 루프를 시도, 당신은
DataTable
,이 경우,
DataSource
을 조회 할 수 있습니다.
dgv는 DataTable과 같은 것입니까? WHERE 절이 Part_No = 'Part A'' 인 경우 Part B와 C는 무엇을하고 있습니까? – Plutonix
@Plutonix 죄송합니다, 분명치 않다. 예, datagridview는 데이터 소스에 바인딩됩니다. 내가 언급 한 where 절은 각 행을 반복 할 때 쿼리에 사용됩니다. – crimson589