GrapeCity CalendarGrid for Windows Forms 2.0J
セルの値をドラッグ&ドロップで移動する

セルの値をドラッグ&ドロップで移動する動作は、GcCalendarGridのMouseDownイベント、DragDropイベント、DragOverイベントを使用して実現できます。

Imports GrapeCity.Win.CalendarGrid

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GcCalendarGrid1.AllowDrop = True

    AddHandler GcCalendarGrid1.MouseDown, AddressOf gcCalendarGrid1_MouseDown
    AddHandler GcCalendarGrid1.DragDrop, AddressOf gcCalendarGrid1_DragDrop
    AddHandler GcCalendarGrid1.DragOver, AddressOf GcCalendarGrid1_DragOver

    ' サンプルデータを作成します。
    Dim today As DateTime = DateTime.Today
    GcCalendarGrid1.Content(today).Rows(1).Cells(0).Value = "サンプルデータ"
    GcCalendarGrid1.ScrollIntoView(today)
End Sub

Private Sub GcCalendarGrid1_MouseDown(sender As Object, e As MouseEventArgs)
    Dim gcCalendarGrid = DirectCast(sender, GcCalendarGrid)
    Dim hitTestInfo As CalendarHitTestInfo = gcCalendarGrid.HitTest(New Point(e.X, e.Y))
    If hitTestInfo.HitTestType = CalendarHitTestType.Content Then
        Dim sourceCellPosition As CalendarCellPosition = hitTestInfo.CellPosition
        gcCalendarGrid.DoDragDrop(sourceCellPosition, DragDropEffects.Move)
    End If
End Sub

Private Sub gcCalendarGrid1_DragDrop(sender As Object, e As DragEventArgs)
    Dim gcCalendarGrid = TryCast(sender, GcCalendarGrid)

    Dim clientPoint As Point = gcCalendarGrid.PointToClient(New Point(e.X, e.Y))
    Dim hitTestInfo As CalendarHitTestInfo = gcCalendarGrid.HitTest(New Point(clientPoint.X, clientPoint.Y))

    Dim sourceCellPosition As CalendarCellPosition = DirectCast(e.Data.GetData(GetType(CalendarCellPosition)), CalendarCellPosition)
    Dim destCellPosition As CalendarCellPosition = hitTestInfo.CellPosition

    If sourceCellPosition <> destCellPosition Then
        MoveCellValue(gcCalendarGrid, sourceCellPosition, destCellPosition)
    End If
End Sub

Private Sub MoveCellValue(gcCalendarGrid As GcCalendarGrid, source As CalendarCellPosition, dest As CalendarCellPosition)
    gcCalendarGrid.Content(dest.Date).Rows(dest.RowIndex).Cells(dest.ColumnIndex).Value = gcCalendarGrid.Content(source.Date).Rows(source.RowIndex).Cells(source.ColumnIndex).Value
    gcCalendarGrid.Content(source.Date).Rows(source.RowIndex).Cells(source.ColumnIndex).Value = Nothing
End Sub

Private Sub GcCalendarGrid1_DragOver(sender As Object, e As DragEventArgs)
    e.Effect = DragDropEffects.Move
End Sub
using GrapeCity.Win.CalendarGrid;

private void Form1_Load(object sender, EventArgs e)
{
    gcCalendarGrid1.AllowDrop = true;
    gcCalendarGrid1.MouseDown += gcCalendarGrid1_MouseDown;
    gcCalendarGrid1.DragDrop += gcCalendarGrid1_DragDrop;
    gcCalendarGrid1.DragOver += gcCalendarGrid1_DragOver;

    // サンプルデータを作成します。
    var today = DateTime.Today;
    gcCalendarGrid1.Content[today].Rows[1].Cells[0].Value = "サンプルデータ";
    gcCalendarGrid1.ScrollIntoView(today);
}

void gcCalendarGrid1_MouseDown(object sender, MouseEventArgs e)
{
    var gcCalendarGrid = sender as GcCalendarGrid;
    CalendarHitTestInfo hitTestInfo = gcCalendarGrid.HitTest(new Point(e.X, e.Y));
    if (hitTestInfo.HitTestType == CalendarHitTestType.Content)
    {
        CalendarCellPosition sourceCellPosition = hitTestInfo.CellPosition;
        gcCalendarGrid.DoDragDrop(sourceCellPosition, DragDropEffects.Move);
    }
}

void gcCalendarGrid1_DragDrop(object sender, DragEventArgs e)
{
    var gcCalendarGrid = sender as GcCalendarGrid;

    Point clientPoint = gcCalendarGrid.PointToClient(new Point(e.X, e.Y));
    CalendarHitTestInfo hitTestInfo = gcCalendarGrid.HitTest(new Point(clientPoint.X, clientPoint.Y));

    CalendarCellPosition sourceCellPosition = (CalendarCellPosition)e.Data.GetData(typeof(CalendarCellPosition));
    CalendarCellPosition destCellPosition = hitTestInfo.CellPosition;

    if (sourceCellPosition != destCellPosition)
    {
        MoveCellValue(gcCalendarGrid, sourceCellPosition, destCellPosition);
    }
}

private void MoveCellValue(GcCalendarGrid gcCalendarGrid, CalendarCellPosition source, CalendarCellPosition dest)
{
    gcCalendarGrid.Content[dest.Date].Rows[dest.RowIndex].Cells[dest.ColumnIndex].Value = gcCalendarGrid.Content[source.Date].Rows[source.RowIndex].Cells[source.ColumnIndex].Value;
    gcCalendarGrid.Content[source.Date].Rows[source.RowIndex].Cells[source.ColumnIndex].Value = null;
}

void gcCalendarGrid1_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}
参照

 

 


© 2008 GrapeCity inc. All rights reserved.