Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions VB/GridControlCRUD.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "GridControlCRUDSimple", "GridControlCRUDSimple\GridControlCRUDSimple.vbproj", "{9E2883B9-744A-48BF-B25A-EA4B2D435C9D}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "GridControlCRUDMVVM", "GridControlCRUDMVVM\GridControlCRUDMVVM.vbproj", "{69E85A61-E7D4-4C9D-A433-13E742AE30BC}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "GridControlCRUDMVVMAsync", "GridControlCRUDMVVMAsync\GridControlCRUDMVVMAsync.vbproj", "{659194A7-745D-4A56-A683-B150B9170F53}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9E2883B9-744A-48BF-B25A-EA4B2D435C9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E2883B9-744A-48BF-B25A-EA4B2D435C9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E2883B9-744A-48BF-B25A-EA4B2D435C9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E2883B9-744A-48BF-B25A-EA4B2D435C9D}.Release|Any CPU.Build.0 = Release|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Release|Any CPU.Build.0 = Release|Any CPU
{659194A7-745D-4A56-A683-B150B9170F53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{659194A7-745D-4A56-A683-B150B9170F53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{659194A7-745D-4A56-A683-B150B9170F53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{659194A7-745D-4A56-A683-B150B9170F53}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51597E69-ADA1-4945-9493-FC7C9FB65AD2}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions VB/GridControlCRUDMVVM/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
9 changes: 9 additions & 0 deletions VB/GridControlCRUDMVVM/Application.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="GridControlCRUDMVVM.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GridControlCRUDMVVM"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
14 changes: 14 additions & 0 deletions VB/GridControlCRUDMVVM/Application.xaml.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Imports DevExpress.Mvvm
Imports DevExpress.Xpf.Core
Imports System.Windows

Namespace GridControlCRUDMVVM
Partial Public Class App
Inherits Application

Public Sub New()
ApplicationThemeHelper.UpdateApplicationThemeName()
DevExpress.Internal.DbEngineDetector.PatchConnectionStringsAndConfigureEntityFrameworkDefaultConnectionFactory()
End Sub
End Class
End Namespace
154 changes: 154 additions & 0 deletions VB/GridControlCRUDMVVM/Common.View/GridControlCRUDBehavior.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.UI.Interactivity
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Grid
Imports System
Imports System.Windows
Imports System.Windows.Input

Namespace DevExpress.CRUD.View
Public Class GridControlCRUDBehavior
Inherits Behavior(Of TableView)

Public Property OnCreateCommand() As ICommand
Get
Return DirectCast(GetValue(OnCreateCommandProperty), ICommand)
End Get
Set(ByVal value As ICommand)
SetValue(OnCreateCommandProperty, value)
End Set
End Property
Public Shared ReadOnly OnCreateCommandProperty As DependencyProperty = DependencyProperty.Register("OnCreateCommand", GetType(ICommand), GetType(GridControlCRUDBehavior), New PropertyMetadata(Nothing))

Public Property OnUpdateCommand() As ICommand
Get
Return DirectCast(GetValue(OnUpdateCommandProperty), ICommand)
End Get
Set(ByVal value As ICommand)
SetValue(OnUpdateCommandProperty, value)
End Set
End Property
Public Shared ReadOnly OnUpdateCommandProperty As DependencyProperty = DependencyProperty.Register("OnUpdateCommand", GetType(ICommand), GetType(GridControlCRUDBehavior), New PropertyMetadata(Nothing))

Public Property OnDeleteCommand() As ICommand
Get
Return DirectCast(GetValue(OnDeleteCommandProperty), ICommand)
End Get
Set(ByVal value As ICommand)
SetValue(OnDeleteCommandProperty, value)
End Set
End Property
Public Shared ReadOnly OnDeleteCommandProperty As DependencyProperty = DependencyProperty.Register("OnDeleteCommand", GetType(ICommand), GetType(GridControlCRUDBehavior), New PropertyMetadata(Nothing))

Public Property OnRefreshCommand() As ICommand
Get
Return DirectCast(GetValue(OnRefreshCommandProperty), ICommand)
End Get
Set(ByVal value As ICommand)
SetValue(OnRefreshCommandProperty, value)
End Set
End Property
Public Shared ReadOnly OnRefreshCommandProperty As DependencyProperty = DependencyProperty.Register("OnRefreshCommand", GetType(ICommand), GetType(GridControlCRUDBehavior), New PropertyMetadata(Nothing))

Public Property NoRecordsErrorMessage() As String
Get
Return CStr(GetValue(NoRecordsErrorMessageProperty))
End Get
Set(ByVal value As String)
SetValue(NoRecordsErrorMessageProperty, value)
End Set
End Property
Public Shared ReadOnly NoRecordsErrorMessageProperty As DependencyProperty = DependencyProperty.Register("NoRecordsErrorMessage", GetType(String), GetType(GridControlCRUDBehavior), New PropertyMetadata(Nothing, Sub(d, e) (CType(d, GridControlCRUDBehavior)).UpdateErrorText()))

Public ReadOnly Property DeleteCommand() As ICommand
Public ReadOnly Property RefreshCommand() As ICommand

Private ReadOnly Property View() As TableView
Get
Return AssociatedObject
End Get
End Property

Public Sub New()
DeleteCommand = New DelegateCommand(AddressOf DoDelete, AddressOf CanDelete)
RefreshCommand = New DelegateCommand(AddressOf DoRefresh, AddressOf CanRefresh)
End Sub

Protected Overrides Sub OnAttached()
MyBase.OnAttached()
AddHandler View.ValidateRow, AddressOf OnValidateRow
AddHandler View.PreviewKeyDown, AddressOf OnPreviewKeyDown
UpdateErrorText()
End Sub

Protected Overrides Sub OnDetaching()
RemoveHandler View.ValidateRow, AddressOf OnValidateRow
RemoveHandler View.PreviewKeyDown, AddressOf OnPreviewKeyDown
UpdateErrorText()
MyBase.OnDetaching()
End Sub

Private Sub UpdateErrorText()
If View Is Nothing Then
Return
End If
If NoRecordsErrorMessage IsNot Nothing Then
View.ShowEmptyText = True
View.RuntimeLocalizationStrings = New GridRuntimeStringCollection() From {New RuntimeStringIdInfo(GridControlRuntimeStringId.NoRecords, NoRecordsErrorMessage)}
Else
View.ShowEmptyText = False
View.RuntimeLocalizationStrings = Nothing
End If
End Sub

Private Sub OnPreviewKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.Key = Key.Delete AndAlso CanDelete() Then
DoDelete()
e.Handled = True
End If
If e.Key = Key.F5 AndAlso CanRefresh() Then
DoRefresh()
e.Handled = True
End If
End Sub

Private Sub DoRefresh()
OnRefreshCommand.Execute(Nothing)
End Sub
Private Function CanRefresh() As Boolean
Return OnRefreshCommand IsNot Nothing AndAlso Not IsEditingRowState()
End Function

Private Sub DoDelete()
Dim row = View.Grid.SelectedItem
If row Is Nothing Then
Return
End If
If DXMessageBox.Show(View, "Are you sure you want to delete this row?", "Delete Row", MessageBoxButton.OKCancel) = MessageBoxResult.Cancel Then
Return
End If
Try
OnDeleteCommand.Execute(row)
View.Commands.DeleteFocusedRow.Execute(Nothing)
Catch ex As Exception
DXMessageBox.Show(ex.Message)
End Try
End Sub

Private Function CanDelete() As Boolean
Return OnDeleteCommand IsNot Nothing AndAlso Not IsEditingRowState()
End Function

Private Function IsEditingRowState() As Boolean
Return View?.AreUpdateRowButtonsShown = True
End Function

Private Sub OnValidateRow(ByVal sender As Object, ByVal e As GridRowValidationEventArgs)
If View.FocusedRowHandle = DataControlBase.NewItemRowHandle Then
OnCreateCommand.Execute(e.Row)
Else
OnUpdateCommand.Execute(e.Row)
End If
End Sub
End Class
End Namespace
56 changes: 56 additions & 0 deletions VB/GridControlCRUDMVVM/Common.ViewModel/CollectionViewModel.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Imports DevExpress.CRUD.DataModel
Imports DevExpress.Mvvm
Imports System.Collections.Generic
Imports System.Windows.Input

Namespace DevExpress.CRUD.ViewModel
Public MustInherit Class CollectionViewModel(Of T As Class)
Inherits ViewModelBase

Private ReadOnly dataProvider As ICRUDDataProvider(Of T)

Protected Sub New(ByVal dataProvider As ICRUDDataProvider(Of T))
Me.dataProvider = dataProvider
OnRefresh()
OnRefreshCommand = New DelegateCommand(AddressOf OnRefresh)
OnCreateCommand = New DelegateCommand(Of T)(AddressOf Me.dataProvider.Create)
OnUpdateCommand = New DelegateCommand(Of T)(AddressOf Me.dataProvider.Update)
OnDeleteCommand = New DelegateCommand(Of T)(AddressOf Me.dataProvider.Delete)
End Sub

Public ReadOnly Property OnRefreshCommand() As ICommand
Public ReadOnly Property OnCreateCommand() As ICommand(Of T)
Public ReadOnly Property OnUpdateCommand() As ICommand(Of T)
Public ReadOnly Property OnDeleteCommand() As ICommand(Of T)

Public Property Entities() As IList(Of T)
Get
Return GetValue(Of IList(Of T))()
End Get
Private Set(ByVal value As IList(Of T))
SetValue(value)
End Set
End Property
Public Property EntitiesErrorMessage() As String
Get
Return GetValue(Of String)()
End Get
Private Set(ByVal value As String)
SetValue(value)
End Set
End Property

Private Sub OnRefresh()
Try
Entities = dataProvider.Read()
EntitiesErrorMessage = Nothing
Catch
Entities = Nothing
EntitiesErrorMessage = "An error has occurred while establishing a connection to the database. Press F5 to retry the connection."
End Try
OnRefreshCore()
End Sub
Protected Overridable Sub OnRefreshCore()
End Sub
End Class
End Namespace
24 changes: 24 additions & 0 deletions VB/GridControlCRUDMVVM/GridControlCRUDMVVM.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "GridControlCRUDMVVM", "GridControlCRUDMVVM.vbproj", "{69E85A61-E7D4-4C9D-A433-13E742AE30BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69E85A61-E7D4-4C9D-A433-13E742AE30BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51597E69-ADA1-4945-9493-FC7C9FB65AD2}
EndGlobalSection
EndGlobal
Loading