333 lines
15 KiB
VB.net
333 lines
15 KiB
VB.net
Imports DevExpress.XtraGrid
|
|
Imports DevExpress.XtraGrid.Views.Grid
|
|
Imports DDUserManager.UserDataSet
|
|
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
|
|
|
|
''' <summary>
|
|
''' Anmerkungen:
|
|
''' - DateTimePicker sollten Binding auf die Text Eigenschaft erhalten, nicht auf die Value Eigenschaft:
|
|
''' https://stackoverflow.com/questions/21270697/argumentoutofrangeexception-with-data-binding-when-debugger-is-running-vs2010#28550637
|
|
''' </summary>
|
|
Public Class frmMain
|
|
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
|
|
Try
|
|
TBDD_CLIENTTableAdapter.Fill(UserDataSet.TBDD_CLIENT)
|
|
TBDD_USER_MODULESTableAdapter.Fill(UserDataSet.TBDD_USER_MODULES)
|
|
TBDD_USER_GROUPSTableAdapter.Fill(UserDataSet.TBDD_USER_GROUPS)
|
|
TBDD_CLIENT_USERTableAdapter.Fill(UserDataSet.TBDD_CLIENT_USER)
|
|
TBDD_GROUPS_USERTableAdapter.Fill(UserDataSet.TBDD_GROUPS_USER)
|
|
TBDD_USERTableAdapter.Fill(UserDataSet.TBDD_USER)
|
|
Catch ex As Exception
|
|
MessageBox.Show($"Error in frmMain_Load: {ex.Message}")
|
|
End Try
|
|
|
|
gvUsers.BestFitColumns()
|
|
|
|
gvClients_AllClients.BestFitColumns()
|
|
gvGroups_AllGroups.BestFitColumns()
|
|
End Sub
|
|
|
|
Private Sub grvwAllGroups_FocusedRowChanged(sender As Object, e As Views.Base.FocusedRowChangedEventArgs) Handles gvGroups_AllGroups.FocusedRowChanged
|
|
If e.FocusedRowHandle = -1 Then
|
|
Exit Sub
|
|
End If
|
|
|
|
Dim groupId As Integer = GetSelectedGroupId()
|
|
|
|
gridGroups_AssignedUsers.DataSource = GetAssignedUsersByGroupId(groupId)
|
|
gridGroups_AvailableUsers.DataSource = GetAvailableUsersByGroupId(groupId)
|
|
End Sub
|
|
|
|
Private Sub gvClients_AllClients_FocusedRowChanged(sender As Object, e As Views.Base.FocusedRowChangedEventArgs) Handles gvClients_AllClients.FocusedRowChanged
|
|
If e.FocusedRowHandle = -1 Then
|
|
Exit Sub
|
|
End If
|
|
|
|
Dim clientId As Integer = GetSelectedClientId()
|
|
|
|
gridClients_AssignedUsers.DataSource = GetAssignedUsersByClientId(clientId)
|
|
gridClients_AvailableUsers.DataSource = GetAvailableUsersByClientId(clientId)
|
|
End Sub
|
|
|
|
#Region "User Details"
|
|
Private Function GetClientsForUser(username As String) As DataTable
|
|
Try
|
|
Dim dt As New TBDD_CLIENTDataTable()
|
|
TBDD_CLIENTTableAdapter.FillByUsername(dt, username)
|
|
|
|
Return dt
|
|
Catch ex As Exception
|
|
MessageBox.Show($"Error in UpdateClientsForUser: {ex.Message}")
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetGroupsForUser(username As String) As DataTable
|
|
Try
|
|
Dim dt As New TBDD_USER_GROUPSDataTable()
|
|
TBDD_USER_GROUPSTableAdapter.FillByUsername(dt, username)
|
|
|
|
Return dt
|
|
Catch ex As Exception
|
|
MessageBox.Show($"Error in UpdateGroupsForUser: {ex.Message}")
|
|
End Try
|
|
End Function
|
|
|
|
Private Sub TBDD_USERBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBDD_USERBindingNavigatorSaveItem.Click
|
|
Validate()
|
|
TBDD_USERBindingSource.EndEdit()
|
|
TableAdapterManager.UpdateAll(UserDataSet)
|
|
End Sub
|
|
|
|
Private Sub TBDD_USERBindingSource_Update(sender As Object, e As EventArgs) Handles TBDD_USERBindingSource.PositionChanged, TBDD_USERBindingSource.ListChanged
|
|
If USERNAMETextBox.Text <> String.Empty Then
|
|
listGroups.DataSource = GetGroupsForUser(USERNAMETextBox.Text)
|
|
listClients.DataSource = GetClientsForUser(USERNAMETextBox.Text)
|
|
End If
|
|
End Sub
|
|
#End Region
|
|
#Region "Database Helpers"
|
|
Private Function GetAvailableUsersByGroupId(groupId As Integer) As TBDD_USERDataTable
|
|
Dim dt As New TBDD_USERDataTable()
|
|
TBDD_USERTableAdapter.FillByGroupId_NotInGroup(dt, groupId)
|
|
|
|
Return dt
|
|
End Function
|
|
|
|
Private Function GetAssignedUsersByGroupId(groupId As Integer) As TBDD_USERDataTable
|
|
Dim dt As New TBDD_USERDataTable()
|
|
TBDD_USERTableAdapter.FillByGroupId(dt, groupId)
|
|
|
|
Return dt
|
|
End Function
|
|
|
|
Private Function GetAvailableUsersByClientId(clientId As Integer) As TBDD_USERDataTable
|
|
Dim dt As New TBDD_USERDataTable()
|
|
TBDD_USERTableAdapter.FillByClientId_NotInClient(dt, clientId)
|
|
|
|
Return dt
|
|
End Function
|
|
|
|
Private Function GetAssignedUsersByClientId(clientId As Integer) As TBDD_USERDataTable
|
|
Dim dt As New TBDD_USERDataTable()
|
|
TBDD_USERTableAdapter.FillByClientId(dt, clientId)
|
|
|
|
Return dt
|
|
End Function
|
|
#End Region
|
|
#Region "Grid Helpers"
|
|
Private Function GetSelectedGroup() As TBDD_USER_GROUPSRow
|
|
Dim groupRowIndex = gvGroups_AllGroups.GetSelectedRows().First()
|
|
Dim selectedGroupView As DataRowView = gvGroups_AllGroups.GetRow(groupRowIndex)
|
|
Dim selectedGroup As TBDD_USER_GROUPSRow = selectedGroupView.Row
|
|
|
|
Return selectedGroup
|
|
End Function
|
|
|
|
Private Function GetSelectedGroupId() As Integer
|
|
Dim selectedGroup As TBDD_USER_GROUPSRow = GetSelectedGroup()
|
|
Dim groupId As Integer = selectedGroup.GUID
|
|
|
|
Return groupId
|
|
End Function
|
|
|
|
Private Function GetSelectedClient() As TBDD_CLIENTRow
|
|
Dim clientRowIndex = gvClients_AllClients.GetSelectedRows().First()
|
|
Dim selectedClientView As DataRowView = gvClients_AllClients.GetRow(clientRowIndex)
|
|
Dim selectedClient As TBDD_CLIENTRow = selectedClientView.Row
|
|
|
|
Return selectedClient
|
|
End Function
|
|
|
|
Private Function GetSelectedClientId() As Integer
|
|
Dim selectedClient As TBDD_CLIENTRow = GetSelectedClient()
|
|
Dim clientId As Integer = selectedClient.GUID
|
|
|
|
Return clientId
|
|
End Function
|
|
|
|
Private Function GetModifiedRowsFromDragDropData(grid As GridControl, data As IDataObject)
|
|
Dim table As DataTable = grid.DataSource
|
|
Dim selectedGroup As TBDD_USER_GROUPSRow = GetSelectedGroup()
|
|
Dim modifiedRows As New List(Of TBDD_USERRow)
|
|
|
|
If data.GetDataPresent(GetType(GridView)) Then
|
|
Dim view As GridView = data.GetData(GetType(GridView))
|
|
Dim selectedRows() As Integer = view.GetSelectedRows()
|
|
|
|
If selectedRows.Length = 0 Then
|
|
Return modifiedRows
|
|
End If
|
|
|
|
' Die ausgewählten Rows auslesen und gridUsersAssigned hinzufügen
|
|
For Each rowIndex As Integer In view.GetSelectedRows()
|
|
Dim rowView As DataRowView = view.GetRow(rowIndex)
|
|
Dim row As TBDD_USERRow = rowView.Row
|
|
|
|
If Not table.Rows.Contains(row.GUID) Then
|
|
modifiedRows.Add(row)
|
|
End If
|
|
Next
|
|
|
|
ElseIf data.GetDataPresent(GetType(TBDD_USERRow)) Then
|
|
Dim row As TBDD_USERRow = data.GetData(GetType(TBDD_USERRow))
|
|
|
|
If Not table.Rows.Contains(row.GUID) Then
|
|
modifiedRows.Add(row)
|
|
End If
|
|
End If
|
|
|
|
Return modifiedRows
|
|
End Function
|
|
#End Region
|
|
#Region "DragDrop Events for Groups & Clients"
|
|
Private downHitInfo As GridHitInfo
|
|
|
|
Private Sub gvUsers_MouseDown(sender As Object, e As MouseEventArgs) Handles gvGroups_AvailableUsers.MouseDown, gvGroups_AssignedUsers.MouseDown, gvClients_AssignedUsers.MouseDown, gvClients_AvailableUsers.MouseDown
|
|
Dim view As GridView = sender
|
|
downHitInfo = Nothing
|
|
Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
|
|
|
|
If ModifierKeys <> Keys.None Then
|
|
Return
|
|
End If
|
|
|
|
If e.Button = MouseButtons.Left And hitInfo.RowHandle >= 0 Then
|
|
downHitInfo = hitInfo
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub gvUsers_MouseMove(sender As Object, e As MouseEventArgs) Handles gvGroups_AvailableUsers.MouseMove, gvGroups_AssignedUsers.MouseMove, gvClients_AssignedUsers.MouseMove, gvClients_AvailableUsers.MouseMove
|
|
Dim view As GridView = sender
|
|
Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
|
|
|
|
If e.Button = MouseButtons.Left And Not IsNothing(downHitInfo) Then
|
|
Dim dragSize As Size = SystemInformation.DragSize
|
|
Dim dragRect As New Rectangle(New Point(downHitInfo.HitPoint.X - dragSize.Width / 2, downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize)
|
|
|
|
' DragRect ist ein kleines Rechteck, dessen Mitte der Punkt ist, wo die Maus geklickt wurde.
|
|
' Es soll verhindern, dass durch schnelles Klicken unbeabsichtigt Drag'n'Drop Operationen initiiert werden
|
|
' Siehe: https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.dragsize(v=vs.110).aspx
|
|
If Not dragRect.Contains(New Point(e.X, e.Y)) Then
|
|
' dragDropData enhält eine einzelne Row oder den kompletten View,
|
|
' jenachdem, wie die Drag'n'Drop Operation gestartet wurde.
|
|
Dim dragDropData As Object
|
|
|
|
' Wenn keine Zeile markiert ist
|
|
If downHitInfo.RowHandle < 0 Then
|
|
Exit Sub
|
|
End If
|
|
|
|
' Wenn zwar eine Zeile markiert ist, aber keine über die Checkbox angehakt wurde,
|
|
' wird die markierte Zeile übergeben.
|
|
' Wenn 1 oder n Zeilen über die Checkbox angehakt wurde, werden diese übergeben
|
|
If view.GetSelectedRows().Length = 0 Then
|
|
Dim row As TBDD_USERRow = view.GetDataRow(downHitInfo.RowHandle)
|
|
dragDropData = row
|
|
Else
|
|
dragDropData = view
|
|
End If
|
|
|
|
view.GridControl.DoDragDrop(dragDropData, DragDropEffects.Move)
|
|
downHitInfo = Nothing
|
|
|
|
DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = True
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub gridUsers_DragOver(sender As Object, e As DragEventArgs) Handles gridGroups_AssignedUsers.DragOver, gridGroups_AvailableUsers.DragOver, gridClients_AssignedUsers.DragOver, gridClients_AvailableUsers.DragOver
|
|
Dim selectedUsersDropped As Boolean = e.Data.GetDataPresent(GetType(GridView))
|
|
Dim singleUserDropped As Boolean = e.Data.GetDataPresent(GetType(TBDD_USERRow))
|
|
|
|
Console.WriteLine($"selectedUsersDropped: {selectedUsersDropped}")
|
|
Console.WriteLine($"singleUserDropped: {singleUserDropped}")
|
|
|
|
If selectedUsersDropped Or singleUserDropped Then
|
|
e.Effect = DragDropEffects.Move
|
|
Else
|
|
e.Effect = DragDropEffects.None
|
|
End If
|
|
End Sub
|
|
#End Region
|
|
#Region "DragDrop Events for Groups"
|
|
Private Sub gridUsersAvailable_DragDrop(sender As Object, e As DragEventArgs) Handles gridGroups_AvailableUsers.DragDrop
|
|
Dim grid As GridControl = sender
|
|
Dim table As DataTable = grid.DataSource
|
|
Dim userRowsToBeDeleted = GetModifiedRowsFromDragDropData(grid, e.Data)
|
|
Dim selectedGroup As TBDD_USER_GROUPSRow = GetSelectedGroup()
|
|
|
|
' Zeilen in Oberfläche und in Datenbank einfügen
|
|
For Each userRow As TBDD_USERRow In userRowsToBeDeleted
|
|
TBDD_GROUPS_USERTableAdapter.Delete(userRow.GUID, selectedGroup.GUID)
|
|
Next
|
|
|
|
listGroups.DataSource = GetGroupsForUser(USERNAMETextBox.Text)
|
|
|
|
' Verfügbare Benutzer aktualisieren und Checkboxen leeren
|
|
gridGroups_AssignedUsers.DataSource = GetAssignedUsersByGroupId(selectedGroup.GUID)
|
|
gridGroups_AvailableUsers.DataSource = GetAvailableUsersByGroupId(selectedGroup.GUID)
|
|
gvGroups_AvailableUsers.ClearSelection()
|
|
gvGroups_AssignedUsers.ClearSelection()
|
|
End Sub
|
|
|
|
Private Sub gridUsersAssigned_DragDrop(sender As Object, e As DragEventArgs) Handles gridGroups_AssignedUsers.DragDrop
|
|
Dim grid As GridControl = sender
|
|
Dim table As DataTable = grid.DataSource
|
|
Dim userRowsToBeInserted = GetModifiedRowsFromDragDropData(grid, e.Data)
|
|
Dim selectedGroup As TBDD_USER_GROUPSRow = GetSelectedGroup()
|
|
|
|
' Zeilen in Oberfläche und in Datenbank einfügen
|
|
For Each userRow As TBDD_USERRow In userRowsToBeInserted
|
|
TBDD_GROUPS_USERTableAdapter.Insert(userRow.GUID, selectedGroup.GUID, $"Assign User {userRow.USERNAME} to Group {selectedGroup.NAME}", Environment.UserName)
|
|
Next
|
|
|
|
listGroups.DataSource = GetGroupsForUser(USERNAMETextBox.Text)
|
|
|
|
' Verfügbare Benutzer aktualisieren und Checkboxen leeren
|
|
gridGroups_AssignedUsers.DataSource = GetAssignedUsersByGroupId(selectedGroup.GUID)
|
|
gridGroups_AvailableUsers.DataSource = GetAvailableUsersByGroupId(selectedGroup.GUID)
|
|
gvGroups_AvailableUsers.ClearSelection()
|
|
gvGroups_AssignedUsers.ClearSelection()
|
|
End Sub
|
|
#End Region
|
|
#Region "DragDrop Events for Clients"
|
|
Private Sub gridClients_AvailableUsers_DragDrop(sender As Object, e As DragEventArgs) Handles gridClients_AvailableUsers.DragDrop
|
|
Dim grid As GridControl = sender
|
|
Dim table As DataTable = grid.DataSource
|
|
Dim userRowsToBeDeleted = GetModifiedRowsFromDragDropData(grid, e.Data)
|
|
Dim selectedClient As TBDD_CLIENTRow = GetSelectedClient()
|
|
|
|
For Each userRow As TBDD_USERRow In userRowsToBeDeleted
|
|
TBDD_CLIENT_USERTableAdapter.Delete(userRow.GUID, selectedClient.GUID)
|
|
Next
|
|
|
|
listClients.DataSource = GetClientsForUser(USERNAMETextBox.Text)
|
|
|
|
' Verfügbare Benutzer aktualisieren und Checkboxen leeren
|
|
gridClients_AssignedUsers.DataSource = GetAssignedUsersByClientId(selectedClient.GUID)
|
|
gridClients_AvailableUsers.DataSource = GetAvailableUsersByClientId(selectedClient.GUID)
|
|
gvClients_AssignedUsers.ClearSelection()
|
|
gvClients_AvailableUsers.ClearSelection()
|
|
End Sub
|
|
|
|
Private Sub gridClients_AssignedUsers_DragDrop(sender As Object, e As DragEventArgs) Handles gridClients_AssignedUsers.DragDrop
|
|
Dim grid As GridControl = sender
|
|
Dim table As DataTable = grid.DataSource
|
|
Dim userRowsToBeInserted = GetModifiedRowsFromDragDropData(grid, e.Data)
|
|
Dim selectedClient As TBDD_CLIENTRow = GetSelectedClient()
|
|
|
|
' Zeilen in Oberfläche und in Datenbank einfügen
|
|
For Each userRow As TBDD_USERRow In userRowsToBeInserted
|
|
TBDD_CLIENT_USERTableAdapter.Insert(userRow.GUID, selectedClient.GUID, $"Assign User {userRow.USERNAME} to Client {selectedClient.CLIENT_NAME}", Environment.UserName)
|
|
Next
|
|
|
|
listClients.DataSource = GetClientsForUser(USERNAMETextBox.Text)
|
|
|
|
' Verfügbare Benutzer aktualisieren und Checkboxen leeren
|
|
gridClients_AssignedUsers.DataSource = GetAssignedUsersByClientId(selectedClient.GUID)
|
|
gridClients_AvailableUsers.DataSource = GetAvailableUsersByClientId(selectedClient.GUID)
|
|
gvClients_AssignedUsers.ClearSelection()
|
|
gvClients_AvailableUsers.ClearSelection()
|
|
End Sub
|
|
#End Region
|
|
End Class |