このチュートリアルでは、True DBGrid for WinForms のドラッグ&ドロップ機能を使用する方法を学びます。
以下の手順を実行します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub C1TrueDBGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseUp
' MouseUpイベントでドラッグ& ドロップフラグをリセットします。
ResetDragDrop()
End Sub
Private Sub C1TrueDBGrid1_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles C1TrueDBGrid1.QueryContinueDrag
' ドラッグ& ドロップ操作を実施/キャンセルした場合は、上のグリッドをリセットします。
If e.Action = DragAction.Drop OrElse e.Action = DragAction.Cancel Then
ResetDragDrop()
End If
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void C1TrueDBGrid1_MouseUp(object sender, MouseEventArgs e)
{
// MouseUpイベントでドラッグ& ドロップフラグをリセットします。
ResetDragDrop();
}
private void C1TrueDBGrid1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
// ドラッグ& ドロップ操作を実施/キャンセルした場合は、上のグリッドをリセットします。
if ((e.Action == DragAction.Drop) || (e.Action == DragAction.Cancel))
this.ResetDragDrop();
}
|
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Dim _ptStartDrag As Point Dim _dragRow As Long |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
Point _ptStartDrag; long _dragRow; |
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Me.CallListTableAdapter.Fill(Me.DsCallList.CallList) Me.CustomersTableAdapter.Fill(Me.DsCustomers.Customers) |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
this.CallListTableAdapter.Fill(this.DsCallList.CallList); this.CustomersTableAdapter.Fill(this.DsCustomers.Customers); |
|

このセクションでは、C1TrueDBGrid1 から C1TrueDBGrid2 にセルまたは行の内容をドラッグするために必要なコードについて説明します。このコードでは、データ行全体を C1TrueDBGrid2 にドラッグして、C1TrueDBGrid2 に新しいレコードを追加するとします。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub ResetDragDrop()
' 強調表示とラベルテキストをリセットして、ドラッグ アンドドロップ操作を取り消します。
Me._ptStartDrag = Point.Empty
Me._dragRow = - 1
Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder
Me.C1TrueDBGrid2.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder
Me.Label3.Text = "Drag a row from the top grid and drop it on the bottom grid."
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void ResetDragDrop()
{
// 強調表示とラベルテキストをリセットして、ドラッグ アンドドロップ操作を取り消します。
this._ptStartDrag = Point.Empty;
this._dragRow = - 1;
this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder;
this.c1TrueDBGrid2.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder;
this.label3.Text = "Drag a row from the top grid and drop it on the bottom grid.";
}
|
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub C1TrueDBGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseDown
Dim row, col As Integer
Me._ptStartDrag = Point.Empty
Me._dragRow = - 1
If Me.C1TrueDBGrid1.CellContaining(e.X, e.Y, row, col) Then
' ドラッグ操作の開始点を保存します。
Me._ptStartDrag = New Point(e.X, e.Y)
Me._dragRow = row
End If
End Sub
Private Sub C1TrueDBGrid1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseMove
' ドラッグの開始点が空でない場合は、ドラッグが開始されています。
If Not Me._ptStartDrag.IsEmpty Then
' ドラッグ操作の開始点を囲む四角形を 2 ピクセルで作成します。
Dim r As New Rectangle(Me._ptStartDrag, Size.Empty)
r.Inflate(2, 2)
' 3 ピクセル以上移動している場合は、ドラッグ操作を開始します。
If Not r.Contains(e.X, e.Y) Then
Me.C1TrueDBGrid1.Row = Me._dragRow
Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow
Me.C1TrueDBGrid1.DoDragDrop(Me._dragRow, DragDropEffects.Copy)
End If
End If
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void C1TrueDBGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int row, col;
this._ptStartDrag = Point.Empty;
this._dragRow = - 1;
if (this.c1TrueDBGrid1.CellContaining(e.X, e.Y, row, col) )
{
// ドラッグ操作の開始点を保存します。
this._ptStartDrag = new Point(e.X, e.Y);
this._dragRow = row;
}
}
private void C1TrueDBGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// ドラッグの開始点が空でない場合は、ドラッグが開始されています。
if (!this._ptStartDrag.IsEmpty )
{
// ドラッグ操作の開始点を囲む四角形を 2 ピクセルで作成します。
Rectangle r = new Rectangle(this._ptStartDrag, Size.Empty);
r.Inflate(2, 2);
// 3 ピクセル以上移動している場合は、ドラッグ操作を開始します。
if (!r.Contains(e.X, e.Y) )
{
this.c1TrueDBGrid1.Row = this._dragRow;
this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow;
this.c1TrueDBGrid1.DoDragDrop(this._dragRow, DragDropEffects.Copy);
}
}
}
|
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub C1TrueDBGrid2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles C1TrueDBGrid2.DragEnter
Me.Label3.Text = "Create a new record when dropped..."
e.Effect = DragDropEffects.Copy
End Sub
Private Sub C1TrueDBGrid2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles C1TrueDBGrid2.DragDrop
Try
Dim row As Integer = CInt(e.Data.GetData(GetType(Integer)))
' グリッドインデクサを使ってデータを取得します。
Dim custname As String = Me.C1TrueDBGrid1(row, "FirstName").ToString()
' CellText() メソッドを使ってデータを取得します。
custname += " " + Me.C1TrueDBGrid1.Columns("LastName").CellText(row)
' CellValue() メソッドを使ってデータを取得します。
custname += " " + Me.C1TrueDBGrid1.Columns("Company").CellValue(row).ToString()
' 下のグリッドのデータセットに新しい行を追加します。
Dim drv As DataRowView = Me.DsCallList.CallList.DefaultView.AddNew()
drv("CallDate") = DateTime.Now
drv("Customer") = custname
drv("Phone") = Me.C1TrueDBGrid1.Columns("Phone").Value.ToString()
drv.EndEdit()
Me.C1TrueDBGrid2.MoveLast()
Me.C1TrueDBGrid2.Select()
' データベースに変更をコミットします。
Dim inserted As DataSet = Me.DsCallList.GetChanges(DataRowState.Added)
If Not (inserted Is Nothing) Then
Me.CallListTableAdapter.Update(inserted)
End If
Me.ResetDragDrop()
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void C1TrueDBGrid2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
this.label3.Text = "Create a new record when dropped...";
e.Effect = DragDropEffects.Copy;
}
private void C1TrueDBGrid2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
try
{
int row = (int)e.Data.GetData(typeof(int));
// グリッドインデクサを使ってデータを取得します。
string custname = this.c1TrueDBGrid1[row, "FirstName"].ToString();
// CellText() メソッドを使ってデータを取得します。
custname += " " + this.c1TrueDBGrid1.Columns["LastName"].CellText(row);
// CellValue() メソッドを使ってデータを取得します。
custname += " " + this.c1TrueDBGrid1.Columns["Company"].CellValue(row).ToString();
// 下のグリッドのデータセットに新しい行を追加します。
DataRowView drv = this.DsCallList.CallList.DefaultView.AddNew();
drv["CallDate"] = DateTime.Now;
drv["Customer"] = custname;
drv["Phone"] = this.c1TrueDBGrid1.Columns["Phone"].Value.ToString();
drv.EndEdit();
this.c1TrueDBGrid2.MoveLast();
this.c1TrueDBGrid2.Select();
// データベースに変更をコミットします。
DataSet inserted = this.DsCallList.GetChanges(DataRowState.Added);
if (! (inserted == null) )
{
this.CallListTableAdapter.Update(inserted);
}
this.ResetDragDrop();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
|
|

これで、チュートリアル 13 は終了です。