Errore di posizionamento di Drag and Drop su DataGridView

venerdì 16 ottobre 2009 - 15.34

kromax Profilo | Newbie

Salve a tutti,
ho un'applicazione per la gestione dei posti in teatro. La rappresentazione delle poltrone è fatta con un DataGridView in cui i posti liberi hanno il fondo verde e quelli occupati il fondo blu.
L'utente deve poter cliccare col tasto destro su una poltrona e draggarne il contenuto su un'altra che non sia occupata.
Tutto funziona, ma per rilasciare un nominativo su una poltrona devo spostare il mouse sulla riga superiore e su quattro colonne a sinistra di quella desiderata.
In più sembra che la cella sia correttamente selezionata solo nella parte superiore, se il puntatore è sulla metà inferiore - o giù di lì - viene selezionata la cella sottostante.
Qualcuno sa spiegarmi il motivo di questo comportamento?
di seguito il codice per il drag and drop.

Private Sub largeGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles largeGrid.MouseDown
Dim hti As DataGridView.HitTestInfo = largeGrid.HitTest(e.X, e.Y)
If e.Button = Windows.Forms.MouseButtons.Right Then
mouseIsDown = True
txtToolTip.Visible = True
If (hti.Type = DataGrid.HitTestType.Cell) Then
If hti.RowIndex > 0 Then
If largeGrid(hti.ColumnIndex, hti.RowIndex).FormattedValue = "" Then
mouseIsDown = False
Exit Sub
End If
oldDropCol = hti.ColumnIndex
oldDropRow = hti.RowIndex
oldBackColor = largeGrid(hti.ColumnIndex, hti.RowIndex).Style.Tag
largeGrid(hti.ColumnIndex, hti.RowIndex).Style.BackColor = Color.BlanchedAlmond
End If
End If
End If
End Sub

Private Sub largeGrid_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles largeGrid.MouseMove
If mouseIsDown Then
Dim hti As DataGridView.HitTestInfo = largeGrid.HitTest(e.X, e.Y)
If (hti.Type = DataGrid.HitTestType.Cell) Then
If hti.RowIndex > 0 Then
If largeGrid(hti.ColumnIndex, hti.RowIndex).FormattedValue = "" Then
mouseIsDown = False
Exit Sub
End If
largeGrid.DoDragDrop(largeGrid(hti.ColumnIndex, hti.RowIndex).Value.ToString, DragDropEffects.Copy)
End If
End If
End If
mouseIsDown = False
txtToolTip.Visible = False
End Sub

Private Sub largeGrid_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles largeGrid.DragDrop
Dim hti As DataGridView.HitTestInfo = largeGrid.HitTest(e.X, e.Y)
If (hti.Type = DataGrid.HitTestType.Cell) Then
If hti.RowIndex > 0 Then 'Color.BlanchedAlmond
If Not largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.BackColor = Color.RoyalBlue _
And largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.BackColor <> Color.BlanchedAlmond Then
If (largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.BackColor = Nothing Or _
largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.BackColor = Color.Yellow) Then
largeGrid(oldDropCol, oldDropRow).Style.BackColor = Color.RoyalBlue
Exit Sub
End If
largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Value = e.Data.GetData(DataFormats.Text)
Dim cosa As String = largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Value
largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.BackColor = Color.RoyalBlue
largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Style.ForeColor = Color.White
largeGrid(oldDropCol, oldDropRow).Style.BackColor = Color.FromName(oldBackColor)
largeGrid(oldDropCol, oldDropRow).Value = ""
Else
largeGrid(oldDropCol, oldDropRow).Style.BackColor = Color.RoyalBlue
End If
End If
End If
End Sub

Private Sub largeGrid_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles largeGrid.DragEnter
Dim hti As DataGridView.HitTestInfo = largeGrid.HitTest(e.X, e.Y)
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub largeGrid_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles largeGrid.DragOver
Dim hti As DataGridView.HitTestInfo = largeGrid.HitTest(e.X, e.Y)
txtToolTip.Text = largeGrid(hti.ColumnIndex, hti.RowIndex - 1).Tag
txtToolTip.Top = e.Y - 5
txtToolTip.Left = e.X + 5
End Sub

Grazie
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5