{"id":4653,"date":"2020-07-05T20:22:53","date_gmt":"2020-07-05T11:22:53","guid":{"rendered":"http:\/\/kats-eye.net\/info\/?p=4653"},"modified":"2020-07-05T20:22:55","modified_gmt":"2020-07-05T11:22:55","slug":"datagridview-drag-drop","status":"publish","type":"post","link":"https:\/\/kats-eye.net\/info\/2020\/07\/05\/datagridview-drag-drop\/","title":{"rendered":"C# dataGRIDVIEW\u3067\u30c9\u30e9\u30c3\u30b0\uff06\u30c9\u30ed\u30c3\u30d7\u3059\u308b"},"content":{"rendered":"\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u6982\u8981<\/h2>\n\n\n\n<p> \u3000C#  dataGridView \u306e\u30ea\u30b9\u30c8\u9805\u76ee\u3092Drag &amp; Drop \u3067\u79fb\u52d5\u3055\u305b\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u78ba\u8a8d\u3057\u305f\u306e\u3067\u66f8\u304d\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002<br>\u3000\u5b9f\u969b\u306e\u51e6\u7406\u72b6\u6cc1\u306f\u6b21\u306e\u52d5\u753b\u306e\u901a\u308a\u3067\u3059\u3002 <\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/gridViewDD.mp4\"><\/video><figcaption><br><\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u30d7\u30ed\u30b0\u30e9\u30e0<\/h2>\n\n\n\n<p>\u3000\u30d5\u30a9\u30fc\u30e0\u4e0a\u306b dataGridView1 \u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3092\u914d\u7f6e\u3057\u3001\u201cAllowDrop\u201d \u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u201cTrue\u201d\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u4e88\u3081\u6b21\u56f3\u306e\u69d8\u306b\u5217\u3092\u7de8\u96c6\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"469\" height=\"309\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/103.png\" alt=\"\" class=\"wp-image-4659\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/103.png 469w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/103-300x198.png 300w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/figure>\n\n\n\n<p>\u3000\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n<pre class=\"height-set:true lang:c# decode:true   \" title=\"DataGridViewDragDrop.sln\">using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\n\nnamespace DataGridViewDragDrop\n{\n    public partial class Form1 : Form\n    {\n        \/\/ https:\/\/www.inforbiro.com\/blog\/c-datagridview-drag-and-drop-rows-reorder\n\n        private Rectangle dragBoxFromMouseDown;\n        private int rowIndexFromMouseDown;\n        private int rowIndexOfItemUnderMouseToDrop;\n\n\n        public Form1()\n        {\n            InitializeComponent();\n            gridview1_init();\n        }\n\n\n        private void gridview1_init() {\n            DataTable table = new DataTable(\"Table\");\n\n            dataGridView1.Rows.Add(\"\u308a\u3093\u3054\", \"100\", \"\u9752\u68ee\u7523\");\n            dataGridView1.Rows.Add(\"\u308a\u3093\u3054\", \"90\", \"\u9577\u91ce\u7523\");\n            dataGridView1.Rows.Add(\"\u3082\u3082\", \"300\", \"\u5ca1\u5c71\u7523 \u5c11\u3057\u50b7\u6709\");\n            dataGridView1.Rows.Add(\"\u304d\u3083\u3079\u3064\", \"110\", \"\u9577\u91ce\u7523\");\n            dataGridView1.Rows.Add(\"\u30bf\u30de\u30cd\u30ae\", \"50\", \"\u5175\u5eab\u7523\");\n            dataGridView1.Rows.Add(\"\u30b8\u30e3\u30ac\u30a4\u30e2\", \"30\", \"\u5317\u6d77\u9053\u7523\");\n            dataGridView1.Rows.Add(\"\u9577\u30cd\u30ae\", \"150\", \"\u57fc\u7389\u7523\");\n            dataGridView1.Rows.Add(\"\u30cb\u30f3\u30b8\u30f3\", \"80\", \"\u5fb3\u5cf6\u7523\");\n            dataGridView1.Rows.Add(\"\u3044\u3061\u3054\", \"400\", \"\u6803\u6728\u7523\");\n        }\n\n\n\n        private void DataGridView1_MouseMove(object sender, MouseEventArgs e)\n        {\n            if ((e.Button &amp; MouseButtons.Left) == MouseButtons.Left)\n            {\n                \/\/ If the mouse moves outside the rectangle, start the drag.\n                if (dragBoxFromMouseDown != Rectangle.Empty &amp;&amp;\n                !dragBoxFromMouseDown.Contains(e.X, e.Y))\n                {\n                    \/\/ Proceed with the drag and drop, passing in the list item.                    \n                    DragDropEffects dropEffect = dataGridView1.DoDragDrop(\n                          dataGridView1.Rows[rowIndexFromMouseDown],\n                          DragDropEffects.Move);\n                }\n            }\n        }\n        private void DataGridView1_MouseDown(object sender, MouseEventArgs e)\n        {\n            \/\/ Get the index of the item the mouse is below.\n            rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;\n\n            if (rowIndexFromMouseDown != -1)\n            {\n                \/\/ Remember the point where the mouse down occurred. \n                \/\/ The DragSize indicates the size that the mouse can move \n                \/\/ before a drag event should be started.                \n                Size dragSize = SystemInformation.DragSize;\n\n                \/\/ Create a rectangle using the DragSize, with the mouse position being\n                \/\/ at the center of the rectangle.\n                dragBoxFromMouseDown = new Rectangle(\n                          new Point(\n                            e.X - (dragSize.Width \/ 2),\n                            e.Y - (dragSize.Height \/ 2)),\n                      dragSize);\n            }\n            else\n                \/\/ Reset the rectangle if the mouse is not over an item in the ListBox.\n                dragBoxFromMouseDown = Rectangle.Empty;\n        }\n\n        private void DataGridView1_DragOver(object sender, DragEventArgs e)\n        {\n            e.Effect = DragDropEffects.Move;\n        }\n\n        private void DataGridView1_DragDrop(object sender, DragEventArgs e)\n        {\n            \/\/ The mouse locations are relative to the screen, so they must be \n            \/\/ converted to client coordinates.\n            Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));\n\n            \/\/ Get the row index of the item the mouse is below. \n            rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;\n\n            \/\/ If the drag operation was a move then remove and insert the row.\n            if (e.Effect == DragDropEffects.Move)\n            {\n                DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;\n\n                if ((rowIndexOfItemUnderMouseToDrop &gt;= 0) &amp;&amp; (rowIndexOfItemUnderMouseToDrop &lt; dataGridView1.Rows.Count - 1))\n                {\n                    Console.WriteLine(rowIndexFromMouseDown);\n                    dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);\n                    dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);\n\n                    for (int row_idx = 0; row_idx &lt; dataGridView1.Rows.Count ; row_idx++) {\n                        Color tmp = Color.White;\n                        if (row_idx == rowIndexOfItemUnderMouseToDrop) {\n                            tmp = Color.LightYellow;\n                        }\n\n                        for (int clmn_idx = 0; clmn_idx &lt; dataGridView1.Columns.Count; clmn_idx++) {\n                            dataGridView1[ clmn_idx, row_idx].Style.BackColor = tmp;\n                        }\n                    }\n\n                    dataGridView1.ClearSelection();\n                }\n            }\n        }\n    }\n}<\/pre>\n<p>\u00a0<\/p>\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">Xbox Game Bar \u753b\u9762\u30ad\u30e3\u30d7\u30c1\u30e3\u30fc<\/h2>\n\n\n\n<p>\u3000\u4eca\u56de\u306e\u30c7\u30e2\u52d5\u753b\u306f\u3001Windows10\u306b\u5165\u3063\u3066\u3044\u308b Xbox Game Bar \u3092\u4f7f\u3044\u307e\u3057\u305f\u3002PC\u753b\u9762\u306e\u9023\u7d9a\u52d5\u753b\u30ad\u30e3\u30d7\u30c1\u30e3\u30fc\u306b\u306f\u5c02\u7528\u30bd\u30d5\u30c8\u304c\u5fc5\u8981\u3068\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u201cXbox Game Bar\u201d \u3068\u3044\u3046\u30bd\u30d5\u30c8\u3067\u51fa\u6765\u308b\u3053\u3068\u3092\u77e5\u308a\u307e\u3057\u305f\u3002\u672c\u984c\u3068\u306f\u76f4\u63a5\u95a2\u4fc2\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u4eca\u5f8c\u3082\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u3001\u624b\u9806\u3092\u6574\u7406\u3057\u307e\u3059\u3002<br>\u3000\u30ad\u30fc\u30dc\u30fc\u30c9\u306e\u300cWindows\uff08\u30ed\u30b4\u30de\u30fc\u30af\uff09\u300d+\u300cG\u300d\u3067\u8d77\u52d5\u3059\u308b\u69d8\u3067\u3059\u304c\u3001\u79c1\u306e\u30d1\u30bd\u30b3\u30f3\u3067\u306f\u7acb\u3061\u4e0a\u304c\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u6b21\u306e\u624b\u9806\u3067\u8d77\u52d5\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u2460 \u201cXbox Game Bar\u201d \u3092\u691c\u7d22<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"392\" height=\"41\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/107.png\" alt=\"\" class=\"wp-image-4669\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/107.png 392w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/107-300x31.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/figure>\n\n\n\n<p>\u2461 \u201c\u958b\u304f\u201d\u3092\u30af\u30ea\u30c3\u30af<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"781\" height=\"354\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/108.png\" alt=\"\" class=\"wp-image-4670\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/108.png 781w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/108-300x136.png 300w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/108-768x348.png 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/figure>\n\n\n\n<p>\u2462 \u4e2d\u592e\u306e \u201c\u25cf\u201d \u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u30ad\u30e3\u30d7\u30c1\u30e3\u30fc\u3092\u958b\u59cb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"274\" height=\"126\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/07\/105.png\" alt=\"\" class=\"wp-image-4668\"\/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p> \u3000\u5c11\u3057\u6c17\u306b\u306a\u3063\u3066\u3044\u305f  (1) dataGridView \u306e\u30c9\u30e9\u30c3\u30b0\uff06\u30c9\u30ed\u30c3\u30d7 \u306b\u3088\u308b\u9806\u756a\u5165\u308c\u66ff\u3048\u3068 (2)PC\u753b\u9762\u9023\u7d9a\u30ad\u30e3\u30d7\u30c1\u30e3\u30fc\u65b9\u6cd5\u306b\u3064\u3044\u3066\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002<br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u3000C# dataGridView \u306e\u30ea\u30b9\u30c8\u9805\u76ee\u3092Drag &amp; Drop \u3067\u79fb\u52d5\u3055\u305b\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u78ba\u8a8d\u3057\u305f\u306e\u3067\u66f8\u304d\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002\u3000\u5b9f\u969b\u306e\u51e6\u7406\u72b6\u6cc1\u306f\u6b21\u306e\u52d5\u753b\u306e\u901a\u308a\u3067\u3059\u3002 \u30d7\u30ed\u30b0\u30e9\u30e0 \u3000\u30d5\u30a9\u30fc\u30e0\u4e0a\u306b dataG &hellip; <a href=\"https:\/\/kats-eye.net\/info\/2020\/07\/05\/datagridview-drag-drop\/\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">C# dataGRIDVIEW\u3067\u30c9\u30e9\u30c3\u30b0\uff06\u30c9\u30ed\u30c3\u30d7\u3059\u308b<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":4658,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75],"tags":[80,135,59],"_links":{"self":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/4653"}],"collection":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/comments?post=4653"}],"version-history":[{"count":16,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/4653\/revisions"}],"predecessor-version":[{"id":4677,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/4653\/revisions\/4677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/media\/4658"}],"wp:attachment":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/media?parent=4653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/categories?post=4653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/tags?post=4653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}