Winform介面開發技巧分享:如何突出顯示WinForms網格控制元件行
阿新 • • 發佈:2020-08-07
DevExpress Winforms Controls內建140多個UI控制元件和庫,完美構建流暢、美觀且易於使用的應用程式。
遇到的問題
在VB Windows Form上工作,有一下VB程式碼,該程式碼應該為帶有NextCalibrationDate <= to today's date的行提供紅色背景(在此情況下只有兩行),但是沒有實現。當除錯應用程式時,似乎程式碼正在執行,但兩行的顏色未更改為紅色。目前想知道是否是因為在這些單元格中設定了日期的方式。
Private Sub GridView1_RowStyle(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle Dim nextCalibDate As Date Dim I As Integer Dim DataRowCount As Integer = GridView1.DataRowCount Dim View As GridView = sender For I = 0 To DataRowCount - 1 If IsDBNull(GridView1.GetRowCellValue(e.RowHandle, colNextCalibrationDt)) Then 'Nothing Else nextCalibDate = GridView1.GetRowCellValue(e.RowHandle, colNextCalibrationDt) If nextCalibDate <= Today.Date Then If (e.RowHandle >= 0) Then e.Appearance.BackColor = Color.Red End If End If End If Next End Sub
僅供參考,這是用來設定NextCalibrationDate的程式碼:
Dim DataRowCount As Integer = GridView1.DataRowCount DataRowCount -= 1 Dim DateCalibrated As Date = GridView1.GetRowCellValue(DataRowCount, colDateCalibrated) Dim NextCalibrationDt As String 'Set NextCalibrationDt Select Case CalibInterval Case "D" NextCalibrationDt = DateAdd(DateInterval.Day, 1, DateCalibrated) Case "W" NextCalibrationDt = DateAdd(DateInterval.Day, 7, DateCalibrated) Case "M" NextCalibrationDt = DateAdd(DateInterval.Month, 1, DateCalibrated) Case "6mos" NextCalibrationDt = DateAdd(DateInterval.Month, 6, DateCalibrated) Case "Y" NextCalibrationDt = DateAdd(DateInterval.Year, 1, DateCalibrated) Case "B" NextCalibrationDt = DateAdd(DateInterval.Year, 2, DateCalibrated) Case "36mos" NextCalibrationDt = DateAdd(DateInterval.Year, 3, DateCalibrated) Case "N" NextCalibrationDt = "NULL" Case "Calibration Not Required" NextCalibrationDt = "NULL" Case Else NextCalibrationDt = "NULL" End Select If NextCalibrationDt <> "NULL" Then NextCalibrationDt = "'" & NextCalibrationDt & "'" End If Dim sqlString As String = "UPDATE [ToolingCalibration].[dbo].[tblToolCalibration] SET LastCalibrationDt = '" & DateCalibrated & "', NextCalibrationDt = " & NextCalibrationDt & " where RecordID = '" & ToolIdToEdit & "'" Dim toolCmd As New SqlCommand(sqlString, connCalibrationRecord) toolCmd.Connection.Open() toolCmd.ExecuteNonQuery() toolCmd.Connection.Close()
解決方案
為了確保滿足您的條件,請在更改e.Appearance.BackColor屬性的行中插入一個斷點。在特定情況下,RowStyle事件提供的外觀設定的優先順序低於其他外觀設定,啟用e.HighPriority選項確保外觀設定具有最高優先順序。此外,由於每個可見行都會引發RowStyle事件,因此您無需迴圈執行程式碼。
在程式碼結尾處設定e.HighPriority = True可以解決問題。
Private Sub GridView1_RowStyle(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle Dim nextCalibDate As Date Dim I As Integer Dim View As GridView = sender If IsDBNull(GridView1.GetRowCellValue(e.RowHandle, colNextCalibrationDt)) Then 'Nothing Else nextCalibDate = GridView1.GetRowCellValue(e.RowHandle, colNextCalibrationDt) If nextCalibDate <= Today.Date Then If (e.RowHandle >= 0) Then e.Appearance.BackColor = Color.Red End If e.HighPriority = True 'override any other formatting End If End If End Sub
DevExpress技術交流群2:775869749歡迎一起進群討論