move a lot of grid stuff into GridControl class

This commit is contained in:
Jonathan Jenne 2023-06-16 11:27:09 +02:00
parent 49239a3e6e
commit 629dfdc304
2 changed files with 189 additions and 248 deletions

View File

@ -572,145 +572,7 @@ Public Class ClassControlCreator
End If End If
oGridControlCreator.ConfigureViewColumns(DT_MY_COLUMNS, oView) oGridControlCreator.ConfigureViewColumns(DT_MY_COLUMNS, oView)
oGridControlCreator.ConfigureViewEvents(DT_MY_COLUMNS, oView, oControl, oControlId)
' Dim oShouldDisplayFooter As Boolean = False
'
'For Each oCol As GridColumn In oView.Columns
' Dim oColumnData As DataRow = DT_MY_COLUMNS.
' Select($"SPALTENNAME = '{oCol.FieldName}'").
' FirstOrDefault()
' If oColumnData Is Nothing Then
' Continue For
' End If
' Dim oSequence As Integer = oColumnData.Item("SEQUENCE")
' oCol.VisibleIndex = oSequence
' Dim oColumnType As String = oColumnData.Item("TYPE_COLUMN")
' Select Case oColumnType
' Case "INTEGER"
' oCol.DisplayFormat.FormatType = FormatType.Custom
' oCol.DisplayFormat.FormatString = "N0"
' Case "DOUBLE"
' oCol.DisplayFormat.FormatType = FormatType.Custom
' oCol.DisplayFormat.FormatString = "N2"
' Case "CURRENCY"
' oCol.DisplayFormat.FormatType = FormatType.Custom
' oCol.DisplayFormat.FormatString = "C2"
' End Select
' Dim oSummaryFunction As String = oColumnData.Item("SUMMARY_FUNCTION")
' Select Case oSummaryFunction
' Case AGGREGATE_TOTAL_INTEGER
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
' oCol.SummaryItem.DisplayFormat = "SUM: {0:N0}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_FLOAT
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
' oCol.SummaryItem.DisplayFormat = "SUM: {0:N2}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_CURRENCY
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum
' oCol.SummaryItem.DisplayFormat = "SUM: {0:C2}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_AVG
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Average
' oCol.SummaryItem.DisplayFormat = "AVG: {0}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_MAX
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Max
' oCol.SummaryItem.DisplayFormat = "MAX: {0}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_MIN
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Min
' oCol.SummaryItem.DisplayFormat = "MIN: {0}"
' oShouldDisplayFooter = True
' Case AGGREGATE_TOTAL_COUNT
' oCol.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count
' oCol.SummaryItem.DisplayFormat = "NUM: {0}"
' oShouldDisplayFooter = True
' End Select
'Next
'
' oView.OptionsView.ShowFooter = oShouldDisplayFooter
AddHandler oView.InitNewRow, Sub(sender As Object, e As InitNewRowEventArgs)
Try
Logger.Debug("Initialzing new row")
For Each oColumnData As DataRow In DT_MY_COLUMNS.Rows
For Each oGridColumn As GridColumn In oView.Columns
If oGridColumn.FieldName <> oColumnData.Item("SPALTENNAME") Then
Continue For
End If
Dim oDefaultValue = NotNull(oColumnData.Item("DEFAULT_VALUE"), String.Empty)
If oDefaultValue <> String.Empty Then
Logger.Debug("Setting default value [{0}] for column [{1}]", oDefaultValue, oGridColumn.FieldName)
oView.SetRowCellValue(e.RowHandle, oGridColumn.FieldName, oDefaultValue)
End If
Next
Next
Catch ex As Exception
Logger.Error(ex)
Finally
newRowModified = False
End Try
End Sub
AddHandler oView.CustomRowCellEdit, Sub(sender As Object, e As CustomRowCellEditEventArgs)
Try
For Each oRow As DataRow In DT_MY_COLUMNS.Rows
Dim oColumnName = oRow.Item("SPALTENNAME")
Dim oEditorExists = GridTables_TestEditorExistsByControlAndColumn(oControlId, oColumnName)
If oColumnName <> e.Column.FieldName Then
Continue For
End If
If oEditorExists Then
Dim oEditor = GridTables.Item(oControlId).Item(oColumnName)
Logger.Debug("Assigning Editor to Column [{0}]", oColumnName)
e.RepositoryItem = oEditor
Else
Logger.Debug("Editor for Column [{0}] does not exist", oColumnName)
End If
Next
Catch ex As Exception
Logger.Warn("Error in CustomRowCellEdit for [{0}]", e.CellValue)
Logger.Error(ex)
End Try
End Sub
AddHandler oView.ValidatingEditor, Sub(sender As Object, e As BaseContainerValidateEditorEventArgs)
Dim oRow As DataRowView = oView.GetRow(oView.FocusedRowHandle)
Dim oColumnName = oView.FocusedColumn.FieldName
Logger.Debug("Validating Editor for Column [{0}]", oColumnName)
GridTables_ValidateColumn(oView, DT_MY_COLUMNS, oColumnName, e.Value, e.Valid, e.ErrorText)
End Sub
AddHandler oView.InvalidRowException, AddressOf View_InvalidRowException
AddHandler oView.ValidatingEditor, AddressOf View_ValidatingEditor
' These handlers are all used for the custom DefaultValue functionality, additionally some code in the 'InitNewRow' event.
' https://supportcenter.devexpress.com/ticket/details/t1035580/how-to-default-a-value-in-a-column-when-add-new-row-in-data-grid
AddHandler oView.ShowingEditor, AddressOf View_ShowingEditor
AddHandler oView.ShownEditor, AddressOf View_ShownEditor
AddHandler oView.ValidateRow, AddressOf View_ValidateRow
AddHandler oControl.LostFocus, AddressOf Control_LostFocus
' 08.11.2021: Fix editor being empty on first open ' 08.11.2021: Fix editor being empty on first open
oView.FocusInvalidRow() oView.FocusInvalidRow()
@ -720,61 +582,7 @@ Public Class ClassControlCreator
Private newRowModified As Boolean = False Private newRowModified As Boolean = False
Private Sub Control_LostFocus(sender As GridControl, e As EventArgs)
Dim oView2 As GridView = sender.FocusedView
' 19.08.2022:
' Calling UpdateCurrentRow when newRowModified Is true
' leads to some weird jumping of focus in the current cell.
' This seems to fix it.
If newRowModified = False Then
oView2.UpdateCurrentRow()
End If
End Sub
Private Sub View_ShowingEditor(sender As Object, e As CancelEventArgs)
Dim view As GridView = TryCast(sender, GridView)
Logger.Debug("Showing editor.")
If view.IsNewItemRow(view.FocusedRowHandle) AndAlso Not newRowModified Then
Logger.Debug("Adding new row.")
view.AddNewRow()
End If
End Sub
Private Sub View_ShownEditor(sender As Object, e As EventArgs)
Dim view As GridView = TryCast(sender, GridView)
If view.IsNewItemRow(view.FocusedRowHandle) Then
Logger.Debug("Attaching Modified Handler.")
AddHandler view.ActiveEditor.Modified, Sub()
Logger.Debug("Row was modified.")
newRowModified = True
End Sub
End If
End Sub
Private Sub View_ValidateRow(sender As Object, e As ValidateRowEventArgs)
Dim view As GridView = TryCast(sender, GridView)
If view.IsNewItemRow(e.RowHandle) AndAlso Not newRowModified Then
Logger.Debug("Deleting unused row")
view.DeleteRow(e.RowHandle)
End If
Logger.Debug("Validating row. Resetting Modified.")
newRowModified = False
End Sub
Private Sub View_ValidatingEditor(sender As Object, e As BaseContainerValidateEditorEventArgs)
Dim oValue As String = NotNull(e.Value, "")
If oValue.Contains(" | ") Then
oValue = oValue.Split(" | ").ToList().First()
e.Value = oValue
End If
End Sub
Private Sub View_InvalidRowException(sender As Object, e As InvalidRowExceptionEventArgs)
e.ExceptionMode = ExceptionMode.NoAction
End Sub
Public Function CreateExistingLine(row As DataRow, designMode As Boolean) As LineLabel Public Function CreateExistingLine(row As DataRow, designMode As Boolean) As LineLabel
Dim control As LineLabel = CreateBaseControl(New LineLabel(), row, designMode) Dim control As LineLabel = CreateBaseControl(New LineLabel(), row, designMode)
@ -1018,56 +826,8 @@ Public Class ClassControlCreator
End If End If
End Sub End Sub
Private Function GridTables_TestEditorExistsByControlAndColumn(oControlId As Integer, pColumn As String) As Boolean
If GridTables.ContainsKey(oControlId) Then
Dim oContainsKey = GridTables.Item(oControlId).ContainsKey(pColumn)
If oContainsKey AndAlso GridTables.Item(oControlId).Item(pColumn) IsNot Nothing Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Private Function GridTables_ValidateColumn(pView As GridView, pColumnDefinition As DataTable, ColumnName As String, pValue As Object, ByRef pIsValid As Boolean, ByRef pErrorText As String) As Boolean
Dim oColumn As DataRow = (From r As DataRow In pColumnDefinition.Rows
Where r.Item("SPALTENNAME") = ColumnName
Select r).FirstOrDefault()
Dim oGridColumn As GridColumn = (From c As GridColumn In pView.Columns
Where c.FieldName = ColumnName
Select c).FirstOrDefault()
Dim oIsRequired = oColumn.Item("VALIDATION")
Try
Dim oRegex = NotNull(oColumn.Item("REGEX_MATCH"), String.Empty)
Dim oRegexMessage = NotNull(oColumn.Item("REGEX_MESSAGE_DE"), String.Empty)
If oRegex <> String.Empty Then
Dim oMatch = New Regex(oRegex).IsMatch(pValue.ToString)
Dim oDefaultMessage = "Wert entspricht nicht dem gefordertem Format!"
Dim oMessage = IIf(oRegexMessage <> String.Empty, oRegexMessage, oDefaultMessage)
If oMatch = False Then
pErrorText = oMessage
pIsValid = False
Return False
End If
End If
Catch ex As Exception
Logger.Error(ex)
End Try
If oIsRequired And (pValue IsNot Nothing AndAlso pValue.ToString = "") Then
pErrorText = "Spalte muss ausgefüllt werden!"
pIsValid = False
Return False
End If
Return True
End Function
End Class End Class

View File

@ -9,14 +9,21 @@ Imports DigitalData.Modules.EDMI.API.DatabaseWithFallback
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Base Imports DigitalData.Modules.Base
Imports System.ComponentModel Imports System.ComponentModel
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Base
Imports System.Text.RegularExpressions
Namespace ControlCreator Namespace ControlCreator
Public Class GridControl Public Class GridControl
Private ReadOnly _LogConfig As LogConfig Private ReadOnly _LogConfig As LogConfig
Private ReadOnly _Logger As Logger
Private ReadOnly _GridTables As Dictionary(Of Integer, Dictionary(Of String, RepositoryItem)) Private ReadOnly _GridTables As Dictionary(Of Integer, Dictionary(Of String, RepositoryItem))
Private newRowModified As Boolean
Public Sub New(pLogConfig As LogConfig, pGridTables As Dictionary(Of Integer, Dictionary(Of String, RepositoryItem))) Public Sub New(pLogConfig As LogConfig, pGridTables As Dictionary(Of Integer, Dictionary(Of String, RepositoryItem)))
_LogConfig = pLogConfig _LogConfig = pLogConfig
_Logger = pLogConfig.GetLogger()
_GridTables = pGridTables _GridTables = pGridTables
End Sub End Sub
@ -62,15 +69,15 @@ Namespace ControlCreator
Try Try
Dim oComboboxDataTable As DataTable = Nothing Dim oComboboxDataTable As DataTable = Nothing
Dim oColumnName As String = oRow.Item("SPALTENNAME") Dim oColumnName As String = oRow.Item("SPALTENNAME")
LOGGER.Debug("Working on SQL for Column[{0}]...", oColumnName) _LOGGER.Debug("Working on SQL for Column[{0}]...", oColumnName)
If Not clsPatterns.HasComplexPatterns(oSqlCommand) Then If Not clsPatterns.HasComplexPatterns(oSqlCommand) Then
LOGGER.Debug("SQL has no complex patterns!") _LOGGER.Debug("SQL has no complex patterns!")
'oComboboxDataTable = ClassDatabase.Return_Datatable_ConId(oSqlCommand, oConnectionId) 'oComboboxDataTable = ClassDatabase.Return_Datatable_ConId(oSqlCommand, oConnectionId)
oComboboxDataTable = DatabaseFallback.GetDatatable(New GetDatatableOptions(oSqlCommand, DatabaseType.ECM) With { oComboboxDataTable = DatabaseFallback.GetDatatable(New GetDatatableOptions(oSqlCommand, DatabaseType.ECM) With {
.ConnectionId = oConnectionId .ConnectionId = oConnectionId
}) })
Else Else
LOGGER.Debug("...has complex patterns!!") _LOGGER.Debug("...has complex patterns!!")
End If End If
Dim oRepositoryItem = GridTables_GetRepositoryItemForColumn(oColumnName, oComboboxDataTable, oRow.Item("ADVANCED_LOOKUP")) Dim oRepositoryItem = GridTables_GetRepositoryItemForColumn(oColumnName, oComboboxDataTable, oRow.Item("ADVANCED_LOOKUP"))
@ -81,8 +88,8 @@ Namespace ControlCreator
_GridTables.Item(pControlId).Add(oColumnName, oRepositoryItem) _GridTables.Item(pControlId).Add(oColumnName, oRepositoryItem)
End If End If
Catch ex As Exception Catch ex As Exception
LOGGER.Warn("Could not load data for column {0} in control {1}", oRow.Item("SPALTENNAME"), pControlName) _LOGGER.Warn("Could not load data for column {0} in control {1}", oRow.Item("SPALTENNAME"), pControlName)
LOGGER.Error(ex) _LOGGER.Error(ex)
End Try End Try
End If End If
Next Next
@ -209,7 +216,181 @@ Namespace ControlCreator
pGridView.OptionsView.ShowFooter = oShouldDisplayFooter pGridView.OptionsView.ShowFooter = oShouldDisplayFooter
End Sub End Sub
Public Sub ConfigureViewEvents(pColumnTable As DataTable, pGridView As GridView, pControl As Windows.Forms.Control, pControlId As Integer)
AddHandler pGridView.InitNewRow, Sub(sender As Object, e As InitNewRowEventArgs)
Try
_LOGGER.Debug("Initialzing new row")
For Each oColumnData As DataRow In pColumnTable.Rows
For Each oGridColumn As GridColumn In pGridView.Columns
If oGridColumn.FieldName <> oColumnData.Item("SPALTENNAME") Then
Continue For
End If
Dim oDefaultValue = ObjectEx.NotNull(oColumnData.Item("DEFAULT_VALUE"), String.Empty)
If oDefaultValue <> String.Empty Then
_LOGGER.Debug("Setting default value [{0}] for column [{1}]", oDefaultValue, oGridColumn.FieldName)
pGridView.SetRowCellValue(e.RowHandle, oGridColumn.FieldName, oDefaultValue)
End If
Next
Next
Catch ex As Exception
_LOGGER.Error(ex)
Finally
newRowModified = False
End Try
End Sub
AddHandler pGridView.CustomRowCellEdit, Sub(sender As Object, e As CustomRowCellEditEventArgs)
Try
For Each oRow As DataRow In pColumnTable.Rows
Dim oColumnName = oRow.Item("SPALTENNAME")
Dim oEditorExists = GridTables_TestEditorExistsByControlAndColumn(pControlId, oColumnName)
If oColumnName <> e.Column.FieldName Then
Continue For
End If
If oEditorExists Then
Dim oEditor = _GridTables.Item(pControlId).Item(oColumnName)
_LOGGER.Debug("Assigning Editor to Column [{0}]", oColumnName)
e.RepositoryItem = oEditor
Else
_LOGGER.Debug("Editor for Column [{0}] does not exist", oColumnName)
End If
Next
Catch ex As Exception
_LOGGER.Warn("Error in CustomRowCellEdit for [{0}]", e.CellValue)
_LOGGER.Error(ex)
End Try
End Sub
AddHandler pGridView.ValidatingEditor, Sub(sender As Object, e As BaseContainerValidateEditorEventArgs)
Dim oRow As DataRowView = pGridView.GetRow(pGridView.FocusedRowHandle)
Dim oColumnName = pGridView.FocusedColumn.FieldName
_LOGGER.Debug("Validating Editor for Column [{0}]", oColumnName)
GridTables_ValidateColumn(pGridView, pColumnTable, oColumnName, e.Value, e.Valid, e.ErrorText)
End Sub
AddHandler pGridView.InvalidRowException, AddressOf View_InvalidRowException
AddHandler pGridView.ValidatingEditor, AddressOf View_ValidatingEditor
' These handlers are all used for the custom DefaultValue functionality, additionally some code in the 'InitNewRow' event.
' https://supportcenter.devexpress.com/ticket/details/t1035580/how-to-default-a-value-in-a-column-when-add-new-row-in-data-grid
AddHandler pGridView.ShowingEditor, AddressOf View_ShowingEditor
AddHandler pGridView.ShownEditor, AddressOf View_ShownEditor
AddHandler pGridView.ValidateRow, AddressOf View_ValidateRow
AddHandler pControl.LostFocus, AddressOf Control_LostFocus
End Sub
Private Sub Control_LostFocus(sender As DevExpress.XtraGrid.GridControl, e As EventArgs)
Dim oView2 As GridView = sender.FocusedView
' 19.08.2022:
' Calling UpdateCurrentRow when newRowModified Is true
' leads to some weird jumping of focus in the current cell.
' This seems to fix it.
If newRowModified = False Then
oView2.UpdateCurrentRow()
End If
End Sub
Private Sub View_ShowingEditor(sender As Object, e As CancelEventArgs)
Dim view As GridView = TryCast(sender, GridView)
_LOGGER.Debug("Showing editor.")
If view.IsNewItemRow(view.FocusedRowHandle) AndAlso Not newRowModified Then
_LOGGER.Debug("Adding new row.")
view.AddNewRow()
End If
End Sub
Private Sub View_ShownEditor(sender As Object, e As EventArgs)
Dim view As GridView = TryCast(sender, GridView)
If view.IsNewItemRow(view.FocusedRowHandle) Then
_LOGGER.Debug("Attaching Modified Handler.")
AddHandler view.ActiveEditor.Modified, Sub()
_LOGGER.Debug("Row was modified.")
newRowModified = True
End Sub
End If
End Sub
Private Sub View_ValidateRow(sender As Object, e As ValidateRowEventArgs)
Dim view As GridView = TryCast(sender, GridView)
If view.IsNewItemRow(e.RowHandle) AndAlso Not newRowModified Then
_LOGGER.Debug("Deleting unused row")
view.DeleteRow(e.RowHandle)
End If
_LOGGER.Debug("Validating row. Resetting Modified.")
newRowModified = False
End Sub
Private Sub View_ValidatingEditor(sender As Object, e As BaseContainerValidateEditorEventArgs)
Dim oValue As String = ObjectEx.NotNull(e.Value, "")
If oValue.Contains(" | ") Then
oValue = oValue.Split(" | ").ToList().First()
e.Value = oValue
End If
End Sub
Private Sub View_InvalidRowException(sender As Object, e As InvalidRowExceptionEventArgs)
e.ExceptionMode = ExceptionMode.NoAction
End Sub
Private Function GridTables_TestEditorExistsByControlAndColumn(oControlId As Integer, pColumn As String) As Boolean
If _GridTables.ContainsKey(oControlId) Then
Dim oContainsKey = _GridTables.Item(oControlId).ContainsKey(pColumn)
If oContainsKey AndAlso _GridTables.Item(oControlId).Item(pColumn) IsNot Nothing Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Private Function GridTables_ValidateColumn(pView As GridView, pColumnDefinition As DataTable, ColumnName As String, pValue As Object, ByRef pIsValid As Boolean, ByRef pErrorText As String) As Boolean
Dim oColumn As DataRow = (From r As DataRow In pColumnDefinition.Rows
Where r.Item("SPALTENNAME") = ColumnName
Select r).FirstOrDefault()
Dim oGridColumn As GridColumn = (From c As GridColumn In pView.Columns
Where c.FieldName = ColumnName
Select c).FirstOrDefault()
Dim oIsRequired = oColumn.Item("VALIDATION")
Try
Dim oRegex = ObjectEx.NotNull(oColumn.Item("REGEX_MATCH"), String.Empty)
Dim oRegexMessage = ObjectEx.NotNull(oColumn.Item("REGEX_MESSAGE_DE"), String.Empty)
If oRegex <> String.Empty Then
Dim oMatch = New Regex(oRegex).IsMatch(pValue.ToString)
Dim oDefaultMessage = "Wert entspricht nicht dem gefordertem Format!"
Dim oMessage = IIf(oRegexMessage <> String.Empty, oRegexMessage, oDefaultMessage)
If oMatch = False Then
pErrorText = oMessage
pIsValid = False
Return False
End If
End If
Catch ex As Exception
_LOGGER.Error(ex)
End Try
If oIsRequired And (pValue IsNot Nothing AndAlso pValue.ToString = "") Then
pErrorText = "Spalte muss ausgefüllt werden!"
pIsValid = False
Return False
End If
Return True
End Function
End Class End Class
End Namespace End Namespace