Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > C# WinApp ขอแนวทางในการ DragDrop ใน DataGridView หน่อยครับ


 

[.NET] C# WinApp ขอแนวทางในการ DragDrop ใน DataGridView หน่อยครับ

 
Topic : 125745



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook


1311

ไม่แน่ใจเหมือนกันนะครับว่าเขาจะเรียก DragDrop ไม๊
จากรูป คือผมมี รหัสตัวอย่าง เช่น W 0002(ฝั่งซ้าย)พอคลิกก็จะปรากฎ parameter หรือรายการวิเคราะห์(ฝั่งขวา)

ทีนี้ User อยากให้ลาก parameter มาลงในรหัสตัวอย่างได้ด้วยครับ

อย่าง เช่น W 0002 มีรายการวิเคราะห์ Arsenic Cadmium Copper Lead Nikel


W 0070 ก็มีรายการวิเคราะห์ Arsenic Cadmium Copper Lead Nikel(เหมือน W 0002 เลย)


คือผมอยากคลิกที่ datagrid ของ รายการวิเคราะห์ ใน W 0002(ฝั่งขวา)
แล้วลากมาลงใน datagrid ของ Rows W 0070 (ฝั่งซ้าย)

แบบนี้ครับ


ปล. ยังไม่เคยศึกษาเกี่ยวกับ Drag Drop เลยครับนึกว่าชาตินี้จะไม่ได้ใช้ซะอีก



Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x), Windows

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-12-15 15:45:05 By : lamaka.tor View : 1755 Reply : 10
 

 

No. 1



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

1311๘1

คือผมจะใส่ Event click ครับ
แต่ไม่รู้เขาเรียกว่าอะไร

RowHeaderMouseClick กับ ColumnHeaderMouseClick ก็ไม่ได้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-15 15:51:31 By : lamaka.tor
 

 

No. 2



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ติดปัญหาเรื่อง DragEventArgs to RowIndex ครับ(นึกว่าจะฉลุยแล้วซะอีก )
คืออยากทราบ RowIndex ของตาราง sample ครับ
ว่าจะเพิ่มให้ sampleID ตัวไหน
Code (C#)
01.private void dataGridView2_DragDrop(object sender, DragEventArgs e)
02.        {
03.            DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
04.             
05.            rowIndexFromMouseDown = dataGridView2.HitTest(e.X, e.Y).RowIndex;
06.            MessageBox.Show(e.X + " "+ e.Y+ " " + rowIndexFromMouseDown);
07.            
08. 
09.            for (int i = rows.Count-1; i >=0  ; i--)
10.            {
11.                if (!string.IsNullOrEmpty("" + rows[i].Cells[0].Value))
12.                    dataGridView2.Rows.Add(rows[i].Cells[0].Value, rows[i].Cells[1].Value, rows[i].Cells[2].Value);
13.            }
14.          /*  foreach (DataGridViewRow row in rows)
15.            {
16. 
17.                if (!string.IsNullOrEmpty("" + row.Cells[0].Value))
18.                dataGridView2.Rows.Add(row.Cells[0].Value, row.Cells[1].Value, row.Cells[2].Value);
19.                
20.                //dataGridView1.Rows.Remove(row);
21.            }*/
22. 
23. 
24.        }



ประวัติการแก้ไข
2016-12-15 23:17:46
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-15 22:44:22 By : lamaka.tor
 

 

No. 3



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

สงสัยวันนี้ท่าทางจะบ้า

ถามเองแล้วก็ตอบเอง
สรุป ได้แล้วนะครับ

//RowIndex
Code (C#)
1.public static int DragDropRowIndex(this System.Windows.Forms.DataGridView dgv, System.Windows.Forms.DragEventArgs e)
2.        {
3.            int r = 0;
4.            System.Drawing.Point p = dgv.PointToClient(new System.Drawing.Point(e.X, e.Y));
5.            r = dgv.HitTest(p.X, p.Y).RowIndex;
6.            return r;
7.        }


โค้ดบ้านๆ ประมาณนี้ครับครับ
Code (C#)
01.static class Program
02.    {
03.        /// <summary>
04.        /// The main entry point for the application.
05.        /// </summary>
06.        [STAThread]
07.        static void Main()
08.        {
09.            Application.EnableVisualStyles();
10.            Application.SetCompatibleTextRenderingDefault(false);
11.            Application.Run(new frmDragNDrop_2() );
12.        }
13.        public static int DragDropRowIndex(this System.Windows.Forms.DataGridView dgv, System.Windows.Forms.DragEventArgs e)
14.        {
15.            int r = 0;
16.            System.Drawing.Point p = dgv.PointToClient(new System.Drawing.Point(e.X, e.Y));
17.            r = dgv.HitTest(p.X, p.Y).RowIndex;
18.            return r;
19.        }
20.    }




Code (C#)
01.public partial class frmDragNDrop_2 : Form
02.    {
03.        public frmDragNDrop_2()
04.        {
05.            InitializeComponent();
06.            this.dataGridView1.AllowDrop = true;
07.            this.dataGridView2.AllowDrop = true;
08.        }
09.        private int rowIndexFromMouseDown;
10. 
11.        private void frmDragNDrop_2_Load(object sender, EventArgs e)
12.        {
13.            for (int i = 0; i < 10; i++)
14.                dataGridView1.Rows.Add("Test " + i, "Tor " + i);
15.        }
16. 
17.        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
18.        {
19.            dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Copy);
20.        }
21. 
22.        private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
23.        {
24.            DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
25.            foreach (DataGridViewRow row in rows)
26.            {
27.                try
28.                {
29.                    listBox2.Items.Add(row.Cells[1].Value);
30.                }
31.                catch { }
32.            }
33. 
34.        }
35. 
36.        private void listBox2_DragEnter(object sender, DragEventArgs e)
37.        {
38.           if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
39.                e.Effect = DragDropEffects.Copy;
40.        }
41. 
42.        private void dataGridView2_DragDrop(object sender, DragEventArgs e)
43.        {
44.            DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
45.            rowIndexFromMouseDown = dataGridView2.DragDropRowIndex(e);
46.            for (int i = rows.Count-1; i >=0  ; i--)
47.            {
48.                if (!string.IsNullOrEmpty("" + rows[i].Cells[0].Value))
49.                    dataGridView2.Rows.Add(rows[i].Cells[0].Value, rows[i].Cells[1].Value, rows[i].Cells[2].Value);
50.            }
51.        }
52. 
53.        private void dataGridView2_DragEnter(object sender, DragEventArgs e)
54.        {
55.            Text = "dataGridView2_DragEnter";
56.            if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection))) e.Effect = DragDropEffects.Copy;
57.        }
58. 
59.        private void dataGridView2_DragOver(object sender, DragEventArgs e)
60.        {
61.            Text = "dataGridView2_DragOver";
62.            if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection))) e.Effect = DragDropEffects.Copy;
63.        }
64. 
65.        
66.    }


ต่อไปคือ
1311
คลิก ลาก เพื่อเพิ่ม rows แบบ excel ครับ

ประมาณว่า W 0001 เป็นน้ำดื่ม รายการวิเคราะห์ Cadmium Iron Lead
W 0002 เป็นน้ำดื่ม รายการวิเคราะห์ Cadmium Iron Lead

อยากจะให้ User กรอกข้อมูล W 0001 แล้วก็ลากลงมา เป็น W 0002 ได้เลยครับ

ยังหาวิธีใช้เม้ายังไม่ได้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-15 23:39:36 By : lamaka.tor
 

 

No. 4



โพสกระทู้ ( 328 )
บทความ ( 0 )



สถานะออฟไลน์


เดี๋ยวซักหน่อยจะมีคนเข้ามาตอบ เห็นเล่นโพสคุณตลอด
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 00:08:54 By : Luz
 

 

No. 5



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

พูดถึงว่าการลากนี่

ภาษา โปรแกรมเมอร์ มันคือ MouseMove ไม๊ครับ
เห็น MouseEventHandler ก็มีกันแค่นี้
Code
Mouse events occur in the following order: MouseEnter MouseMove MouseHover / MouseDown / MouseWheel MouseUp MouseLeave



ปล.ผมอยากทำให้ Cursor เป็น + เมื่อเราคลิกช่องสุดท้ายเหมือน Excel ด้วยก็แหล่มนะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 09:40:30 By : lamaka.tor
 

 

No. 6



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

1311

สงสัยทำไมพอลากไปแล้ว dataGridView2 ก็เพิ่มข้อมูลไปแล้ว

แต่ทำไม dataGridView1 ยังมีร่องรอยอารยธรรม การ คลิกเลือกอยู่ละครับ

ต้อง เอามันออกตรงไหนครับ

Code (C#)
01.public partial class frmDgvDragDrop : Form
02.    {
03.        public frmDgvDragDrop()
04.        {
05.            InitializeComponent();
06.            this.dataGridView1.AllowDrop = true;
07.            this.dataGridView2.AllowDrop = true;
08.            for (int i = 0; i < 10; i++)
09.                dataGridView1.Rows.Add("Test " + i, "Tor " + i);
10.            dataGridView1.MouseDown+=dataGridView1_MouseDown;
11.            dataGridView2.DragDrop += dataGridView2_DragDrop;
12.            dataGridView2.DragEnter+=dataGridView2_DragEnter;
13.        }
14.        private int rowIndexFromMouseDown;
15. 
16.        private void frmDgvDragDrop_Load(object sender, EventArgs e)
17.        {
18. 
19.        }
20.        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
21.        {
22. 
23.            dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Copy);
24.        }
25.        private void dataGridView2_DragDrop(object sender, DragEventArgs e)
26.        {
27.            DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
28.            rowIndexFromMouseDown = dataGridView2.MouseDownRowIndex(e);
29.            MessageBox.Show("" + rowIndexFromMouseDown);
30.            for (int i = rows.Count - 1; i >= 0; i--)
31.            {
32.                if (!string.IsNullOrEmpty("" + rows[i].Cells[0].Value))
33.                    dataGridView2.Rows.Add(rows[i].Cells[0].Value, rows[i].Cells[1].Value, rows[i].Cells[2].Value);
34.            }
35.            e = null;
36.        }
37. 
38.        private void dataGridView2_DragEnter(object sender, DragEventArgs e)
39.        {
40.            if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection))) e.Effect = DragDropEffects.Copy;
41.        }
42. 
43.    }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 11:46:35 By : lamaka.tor
 

 

No. 7



โพสกระทู้ ( 821 )
บทความ ( 0 )



สถานะออฟไลน์


ขยันมาก ๆ ครับ
นับถือเลย

ตัวด้านหน้าของแถว (row header) ปิดทิ้งไหมครับ
อาจจะสวยขึ้น
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 13:16:08 By : fonfire
 

 

No. 8



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 7 เขียนโดย : fonfire เมื่อวันที่ 2016-12-16 13:16:08
รายละเอียดของการตอบ ::
มันตัดได้รึครับ

โอ๊วววววว ความรู้ใหม่อีกแล้ว
ว่าแต่ตัดไงครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 13:20:00 By : lamaka.tor
 

 

No. 9



โพสกระทู้ ( 821 )
บทความ ( 0 )



สถานะออฟไลน์


.RowHeadersVisible = false
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 13:30:20 By : fonfire
 

 

No. 10



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

เหมือนว่าพอเราใช้

Code
AllowDrop = true


datagridviewcombobox กดเลือกไม่ได้ครับ
ไม่รู้ว่าเกี่ยวกันไม๊
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-16 14:31:00 By : lamaka.tor
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# WinApp ขอแนวทางในการ DragDrop ใน DataGridView หน่อยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่