| ■No55076 (チョーさん さん) に返信
> DataGridView1_MouseDownイベント内でDataGridView1.DoDragDropを行うことで、
> ドラッグ中はDataGridView上で複数選択状態(選択セルが反転表示)を保持したままになっていました。
だから、そういうコードを書いたのに…。(^_^;)
> ドラッグが終了あるいは中断するとセル選択は1セルのみになってしまいますが、
> とりあえずこれはよしとします。
無理矢理戻してみました。これでもまだ不十分な感はありますけれども。
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToAddRows = False
DataGridView1.ColumnCount = 10
DataGridView1.RowCount = 20
DataGridView1.ReadOnly = True
DataGridView1.MultiSelect = True
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles DataGridView1.MouseDown
If CBool(e.Button And MouseButtons.Left) Then
Dim ht = DataGridView1.HitTest(e.X, e.Y)
If ht.Type = DataGridViewHitTestType.Cell AndAlso ht.RowIndex <> -1 And ht.ColumnIndex <> -1 Then
If DataGridView1(ht.ColumnIndex, ht.RowIndex).Selected Then
'選択されたセルの一覧
Dim cellList As New List(Of DataGridViewCell)()
Dim textList As New List(Of String)()
For Each c As DataGridViewCell In Me.DataGridView1.SelectedCells
cellList.Insert(0, c)
textList.Insert(0, String.Format("{0}-{1}", c.RowIndex, c.ColumnIndex))
Next
Dim data As String = Join(textList.ToArray(), vbCrLf)
DataGridView1.DoDragDrop(data, DragDropEffects.All)
BeginInvoke(New Foo(AddressOf ReSelect), cellList)
End If
End If
End If
End Sub
Private Delegate Sub Foo(ByVal selectedCells As IEnumerable(Of DataGridViewCell))
Private Sub ReSelect(ByVal selectedCells As IEnumerable(Of DataGridViewCell))
For Each c In selectedCells
c.Selected = True
Next
End Sub
End Class |