質問するログイン新規登録

質問編集履歴

3

コードを削除

2017/01/24 04:24

投稿

ladybird
ladybird

スコア163

title CHANGED
File without changes
body CHANGED
@@ -8,98 +8,6 @@
8
8
  (各メソッドに設けたデバッグポイントに止まりませんでした。)
9
9
  おそらく初歩的なところですみませんが、よろしくお願いします。
10
10
 
11
- ###該当のソースコード
12
- ```C#
13
- using System;
14
- using System.Collections.Generic;
15
- using System.ComponentModel;
16
- using System.Data;
17
- using System.Drawing;
18
- using System.Text;
19
- using System.Windows.Forms;
20
- using System.Configuration;
21
- using System.Data.OleDb;
22
- using DailyReport.DB;
23
-
24
- namespace DailyReport
25
- {
26
- public partial class UserListForm : BaseForm
27
- {
28
- private Rectangle dragBoxFromMouseDown;
29
- private int rowIndexFromMouseDown;
30
- private int rowIndexOfItemUnderMouseToDrop;
31
-
32
- private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
33
- {
34
- if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
35
- {
36
- // If the mouse moves outside the rectangle, start the drag.
37
- if (dragBoxFromMouseDown != Rectangle.Empty &&
38
- !dragBoxFromMouseDown.Contains(e.X, e.Y))
39
- {
40
- // Proceed with the drag and drop, passing in the list item.
41
- DragDropEffects dropEffect = dataGridView1.DoDragDrop(
42
- dataGridView1.Rows[rowIndexFromMouseDown],
43
- DragDropEffects.Move);
44
- }
45
- }
46
- }
47
-
48
- private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
49
- {
50
- // Get the index of the item the mouse is below.
51
- rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
52
- if (rowIndexFromMouseDown != -1)
53
- {
54
- // Remember the point where the mouse down occurred.
55
- // The DragSize indicates the size that the mouse can move
56
- // before a drag event should be started.
57
- Size dragSize = SystemInformation.DragSize;
58
- // Create a rectangle using the DragSize, with the mouse position being
59
- // at the center of the rectangle.
60
- dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
61
- e.Y - (dragSize.Height / 2)),
62
- dragSize);
63
- }
64
- else
65
- // Reset the rectangle if the mouse is not over an item in the ListBox.
66
- dragBoxFromMouseDown = Rectangle.Empty;
67
- }
68
-
69
- private void dataGridView1_DragOver(object sender, DragEventArgs e)
70
- {
71
- e.Effect = DragDropEffects.Move;
72
- }
73
-
74
- private void dataGridView1_DragDrop(object sender, DragEventArgs e)
75
- {
76
- // The mouse locations are relative to the screen, so they must be
77
- // converted to client coordinates.
78
- Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
79
- // Get the row index of the item the mouse is below.
80
- rowIndexOfItemUnderMouseToDrop =
81
- dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
82
- // If the drag operation was a move then remove and insert the row.
83
- if (e.Effect == DragDropEffects.Move)
84
- {
85
-
86
- DataTable dt = (DataTable)dataGridView1.DataSource;
87
-
88
- object[] rowArray = dt.Rows[rowIndexFromMouseDown].ItemArray;
89
- DataRow row = dt.NewRow();
90
- row.ItemArray = rowArray;
91
-
92
- dt.Rows.RemoveAt(rowIndexFromMouseDown);
93
- dt.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop);
94
- }
95
- }
96
-
97
- <他、ロードイベント等 省略>
98
-
99
- }
100
- }
101
- ```
102
-
103
11
  ###試したこと
104
12
  DataGridViewのプロパティの問題かと思いましたが、該当するものを見つけられませんでした。
105
13
 

2

リンク編集

2017/01/24 04:24

投稿

ladybird
ladybird

スコア163

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ###前提・実現したいこと
2
2
  DataGridView内の行をドラッグ&ドロップで並び替えたいです。
3
3
  下記を参考に、ほぼ同じコードにしているのですが、うまくいかず困っています。
4
- https://social.msdn.microsoft.com/Forums/ja-JP/e59a4043-6298-4653-809f-5c8fcf04f2e6/datagridview?forum=csharpgeneralja
4
+ [リンク内容](https://social.msdn.microsoft.com/Forums/ja-JP/e59a4043-6298-4653-809f-5c8fcf04f2e6/datagridview?forum=csharpgeneralja)
5
5
 
6
6
  ###発生している問題・エラーメッセージ
7
7
  そもそも画面上でドラッグ&ドロップができず、各イベントが生じない。

1

初心者アイコンの追加

2017/01/17 07:02

投稿

ladybird
ladybird

スコア163

title CHANGED
File without changes
body CHANGED
File without changes