- PR -

datagridのD&D

1
投稿者投稿内容
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-15 16:13
お世話になります。

質問は、テキストにデータ入力して、クリックイベントでdatagrid1に
データを入力して(ここまでは出来てます。)、そのdatagrid1から
datagrid2に1で入力したデータをDrag&Dropしてデータを2に反映
する方法です。

ご存知の方よろしくお願いします。
Jubei
ぬし
会議室デビュー日: 2002/03/02
投稿数: 830
お住まい・勤務地: 関西
投稿日時: 2004-04-15 19:15
諸農です。

引用:

そのdatagrid1から
datagrid2に1で入力したデータをDrag&Dropしてデータを2に反映
する方法です。



SDK1.1のControl.DragDrop イベントの解説ページにListBoxを二つ並べて
ドラッグアンドドロップを実現する例があります。
参考までにどうぞ。

ms-help://MS.NETFrameworkSDKv1.1.JA/cpref/html/frlrfsystemwindowsformscontrolclassdragdroptopic.htm


_________________
諸農和岳
Powered by Turbo Delphi & Microsoft Visual Studio 2005

十兵衛@わんくま同盟
http://blogs.wankuma.com/jubei/
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-16 09:07
Jubeiさん、返信ありがとうございます。
実は、それをみてわからなかったりします・・・。
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-16 09:24
ソースはこんな感じになっております・・・。


Private Sub datagrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

If e.Button = MouseButtons.Left Then

Dim lbx As DataGrid = CType(sender, DataGrid)

'ドラッグするアイテムのインデックスを取得する

Dim itemIndex As Int32 = lbx.DataSource(e.X, e.Y)
      (ここで、'System.MissingMemberException'
      のハンドルされていない例外が microsoft.visualbasic.dll で発生しました。
      追加情報 : 型 'DataTable' の既定メンバが見つかりません。
とエラーになります・・。)
 
       If itemIndex < 0 Then
Return
End If 'ドラッグするアイテムの内容を取得する
Dim itemText As DataTable = (lbx.DataSource)

'ドラッグ&ドロップ処理を開始する
Dim dde As DragDropEffects = lbx.DoDragDrop(itemText, DragDropEffects.All)

'ドロップ効果がMoveの時はもとのアイテムを削除する
If dde = DragDropEffects.Move Then
lbx.DataSource.RemoveAt(DataGrid1)
End If
End If
End Sub

かなり検討はずれになってるきもする・・・。
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-16 12:07
Private Sub datagrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

''マウスの左ボタンだけが押されている時のみドラッグできるようにする

If e.Button = MouseButtons.Left Then

Dim lbx As DataGrid = CType(sender, DataGrid)

'ドラッグするアイテムのインデックスを取得する()
Dim itemIndex As Int32 = lbx.DataSource(e.X, e.Y)
If itemIndex < "" Then
Return
End If
'ドラッグするアイテムの内容を取得する
Dim itemText As DataTable = (lbx.DataSource)

'ドラッグ&ドロップ処理を開始する
Dim dde As DragDropEffects = lbx.DoDragDrop(itemText, DragDropEffects.All)

'ドロップ効果がMoveの時はもとのアイテムを削除する
If dde = DragDropEffects.Move Then
lbx.DataSource.RemoveAt(DataGrid1)
End If
End If
End Sub
'DATAGRID2内にドラッグされた時
Private Sub datagrid2_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
'INTであればドロップ効果をMoveにする
If e.Data.GetDataPresent(GetType(Int32)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
'DATAGRID2にドロップされたとき
Private Sub DataGrid2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
'ドロップされたデータを調べる
If e.Data.GetDataPresent(GetType(Int32)) Then
Dim target As DataGrid = CType(sender, DataGrid)
'ドロップされたデータを取得
Dim itemText As Int32 = CInt(e.Data.GetData(GetType(Int32)))
'ドロップされたデータを追加する
target.DataSource.Add(DataGrid2)
End If
End Sub

っとこんな感じに作ってみました・・。でも動かないでしす・・(;;
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-16 12:07
Private Sub datagrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

''マウスの左ボタンだけが押されている時のみドラッグできるようにする

If e.Button = MouseButtons.Left Then

Dim lbx As DataGrid = CType(sender, DataGrid)

'ドラッグするアイテムのインデックスを取得する()
Dim itemIndex As Int32 = lbx.DataSource(e.X, e.Y)
If itemIndex < "" Then
Return
End If
'ドラッグするアイテムの内容を取得する
Dim itemText As DataTable = (lbx.DataSource)

'ドラッグ&ドロップ処理を開始する
Dim dde As DragDropEffects = lbx.DoDragDrop(itemText, DragDropEffects.All)

'ドロップ効果がMoveの時はもとのアイテムを削除する
If dde = DragDropEffects.Move Then
lbx.DataSource.RemoveAt(DataGrid1)
End If
End If
End Sub
'DATAGRID2内にドラッグされた時
Private Sub datagrid2_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
'INTであればドロップ効果をMoveにする
If e.Data.GetDataPresent(GetType(Int32)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
'DATAGRID2にドロップされたとき
Private Sub DataGrid2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
'ドロップされたデータを調べる
If e.Data.GetDataPresent(GetType(Int32)) Then
Dim target As DataGrid = CType(sender, DataGrid)
'ドロップされたデータを取得
Dim itemText As Int32 = CInt(e.Data.GetData(GetType(Int32)))
'ドロップされたデータを追加する
target.DataSource.Add(DataGrid2)
End If
End Sub

っとこんな感じに作ってみました・・。でも動かないでしす・・(;;
mogu
会議室デビュー日: 2004/04/15
投稿数: 6
投稿日時: 2004-04-20 11:10
自己レスです。drag出来るまでになりましたが、datagridにデータをdropしても
上手くいきません・・。ご存知の方ご教授お願いします・・。
ソースは下記です。みづらくてすみません・・。

Private Sub DataGrid1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseMove
Dim lbx As DataGrid = CType(sender, DataGrid)
Dim a As DataTable
Dim b As DataRow
Dim c As DataColumn
Dim rc As DataRowCollection


If e.Button = MouseButtons.Left Then
'MsgBox("DataGrid1.CurrentCell.RowNumber=" &   rrentCell.RowNumber)
'MsgBox("DataGrid1.CurrentCell.ColumnNumber=" & DataGrid1.CurrentCell.ColumnNumber)
a = lbx.DataSource
b = a.Rows(DataGrid1.CurrentRowIndex)
'b = a.Rows(DataGrid1.CurrentCell.RowNumber) 'row単位
'c = b.Item("id")
'Dim itemIndex As Int32 = c.Caption
Dim s As String
Try
s = CStr(b.Item(DataGrid1.CurrentRowIndex))
Catch
s = ""
End Try
'MsgBox(s)
'ドラッグするアイテムの内容を取得する()
'Dim itemText As DataTable = (lbx.DataSource)
'ドラッグ&ドロップ処理を開始する

Dim dde As DragDropEffects = lbx.DoDragDrop(s, DragDropEffects.All)
'MsgBox(dde)
'ドロップ効果がMoveの時はもとのアイテムを削除する
If dde = DragDropEffects.Move Then
lbx.DataSource.RemoveAt(DataGrid1.DataSource)
End If
End If
End Sub
Private Sub DataGrid2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGrid2.DragEnter
'DATAGRID2内にドラッグされた時
'strであればドロップ効果をMoveにする
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
'MsgBox(e.Effect)
End Sub
Private Sub DataGrid2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGrid2.DragDrop
'ドロップされたデータを調べる
If e.Data.GetDataPresent(GetType(String)) Then
Dim target As DataGrid = CType(sender, DataGrid)
'ドロップされたデータを取得
Dim itemText As String = CStr(e.Data.GetData(GetType(String)))
'ドロップされたデータを追加する
target.DataSource.Add(DataGrid2)
'MsgBox(e.Data)
End If
End Sub
Jubei
ぬし
会議室デビュー日: 2002/03/02
投稿数: 830
お住まい・勤務地: 関西
投稿日時: 2004-04-22 19:09
諸農です。

すっかり見落としていました。。すみません。

引用:

moguさんの書き込み (2004-04-20 11:10) より:
自己レスです。drag出来るまでになりましたが、datagridにデータをdropしても
上手くいきません・・。ご存知の方ご教授お願いします・・。
ソースは下記です。みづらくてすみません・・。



かなり苦労されているようですね。。
申し訳ないのですが、私はVB.NETが書けないのです。。

1.ドラッグ元のDataGridのMouseMoveイベントハンドラで
  ドラッグの開始を行います。
2.ドロップされる側のDataGridのDragDropイベントハンドラで
  ドロップを受け付けます。
3.同じアプリ内なので、ドラッグドロップで利用するデータを
  自前クラスで実装すると便利かも。

以下のコードでは、ドラッグドロップされたセルの中身を
ToString()メソッドで文字列値変換してラベルに表示しています。

コード:
「1.の処理」
private void dataGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    {
        DataGridCell dc = ((DataGrid)sender).CurrentCell;
        MyData md = new MyData((DataGrid)sender,dc);
        ((DataGrid)sender).DoDragDrop(md,DragDropEffects.Copy);
    }
}

「2.の処理」
private void dataGrid2_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

private void dataGrid2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(MyData)))
    {
        MyData md = (MyData)e.Data.GetData(typeof(MyData));
        label1.Text = md.datagrid[md.cell].ToString();
    }
}

「3.のクラス」
class MyData
{
    public MyData(DataGrid ADataGrid,DataGridCell ACell)
    {
        datagrid = ADataGrid;
        cell = ACell;
    }
    public DataGrid datagrid;
    public DataGridCell cell;
}



_________________
諸農和岳
Powered by Turbo Delphi & Microsoft Visual Studio 2005

十兵衛@わんくま同盟
http://blogs.wankuma.com/jubei/
1

スキルアップ/キャリアアップ(JOB@IT)