|
I am wanting to implement drag-and-drop
on my DataGridView. I have found several examples of how to do this:
most are similar to this:
http://www.timvw.be/drag-and-drop-on-a-datagridview/
The problem
I have though is that my grid is bound to a data source.
Does anyone
have a C# example of how to drag and drop within the same grid that is
bound to a data source?
tia | | VBCoder13 Wednesday, December 31, 2008 3:45 PM | Hi VBCoder13,
I try it by my side, it seem that the code you provide also can implement drag and drop in the DataGridView which is bound to a data source. Please try the following code.
| privatevoidForm1_Load(objectsender,EventArgse) |
| { |
| DataTabledt1=newDataTable("dt1"); |
| dt1.Columns.Add("dt1_ID"); |
| dt1.Columns.Add("dt1_name"); |
| for(inti=0;i<5;i++) |
| { |
| dt1.Rows.Add("1","dt1_1"); |
| dt1.Rows.Add("2","dt1_2"); |
| } |
| this.dataGridView1.DataSource=dt1; |
|
| this.dataGridView1.AllowDrop=true; |
|
| dataGridView1.MouseDown+=newMouseEventHandler(dataGridView1_MouseDown); |
| dataGridView1.DragDrop+=newDragEventHandler(dataGridView1_DragDrop); |
| dataGridView1.DragEnter+=newDragEventHandler(dataGridView1_DragEnter); |
|
| } |
|
| privatevoiddataGridView1_MouseDown(objectsender,MouseEventArgse) |
| { |
| DataGridView.HitTestInfoinfo=this.dataGridView1.HitTest(e.X,e.Y); |
| if(info.RowIndex!=-1&&info.ColumnIndex!=-1) |
| { |
| Objectvalue=this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value; |
| if(value!=null) |
| { |
| this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value=null; |
| this.DoDragDrop(value,DragDropEffects.Move); |
| } |
| } |
| } |
|
| privatevoiddataGridView1_DragDrop(objectsender,DragEventArgse) |
| { |
| Pointp=this.dataGridView1.PointToClient(newPoint(e.X,e.Y)); |
| DataGridView.HitTestInfoinfo=this.dataGridView1.HitTest(p.X,p.Y); |
| if(info.RowIndex!=-1&&info.ColumnIndex!=-1) |
| { |
| Objectvalue=(Object)e.Data.GetData(typeof(string)); |
| this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value=value; |
| } |
| } |
|
| privatevoiddataGridView1_DragEnter(objectsender,DragEventArgse) |
| { |
| e.Effect=DragDropEffects.Move; |
| } |
Hoping that could help you!
If you have any other question, please feel free to let me know.
Best regards,
Ling
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorWednesday, January 07, 2009 6:06 AM
-
| | Ling Wang Friday, January 02, 2009 12:10 PM | Hi VBCoder13,
I try it by my side, it seem that the code you provide also can implement drag and drop in the DataGridView which is bound to a data source. Please try the following code.
| privatevoidForm1_Load(objectsender,EventArgse) |
| { |
| DataTabledt1=newDataTable("dt1"); |
| dt1.Columns.Add("dt1_ID"); |
| dt1.Columns.Add("dt1_name"); |
| for(inti=0;i<5;i++) |
| { |
| dt1.Rows.Add("1","dt1_1"); |
| dt1.Rows.Add("2","dt1_2"); |
| } |
| this.dataGridView1.DataSource=dt1; |
|
| this.dataGridView1.AllowDrop=true; |
|
| dataGridView1.MouseDown+=newMouseEventHandler(dataGridView1_MouseDown); |
| dataGridView1.DragDrop+=newDragEventHandler(dataGridView1_DragDrop); |
| dataGridView1.DragEnter+=newDragEventHandler(dataGridView1_DragEnter); |
|
| } |
|
| privatevoiddataGridView1_MouseDown(objectsender,MouseEventArgse) |
| { |
| DataGridView.HitTestInfoinfo=this.dataGridView1.HitTest(e.X,e.Y); |
| if(info.RowIndex!=-1&&info.ColumnIndex!=-1) |
| { |
| Objectvalue=this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value; |
| if(value!=null) |
| { |
| this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value=null; |
| this.DoDragDrop(value,DragDropEffects.Move); |
| } |
| } |
| } |
|
| privatevoiddataGridView1_DragDrop(objectsender,DragEventArgse) |
| { |
| Pointp=this.dataGridView1.PointToClient(newPoint(e.X,e.Y)); |
| DataGridView.HitTestInfoinfo=this.dataGridView1.HitTest(p.X,p.Y); |
| if(info.RowIndex!=-1&&info.ColumnIndex!=-1) |
| { |
| Objectvalue=(Object)e.Data.GetData(typeof(string)); |
| this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value=value; |
| } |
| } |
|
| privatevoiddataGridView1_DragEnter(objectsender,DragEventArgse) |
| { |
| e.Effect=DragDropEffects.Move; |
| } |
Hoping that could help you!
If you have any other question, please feel free to let me know.
Best regards,
Ling
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorWednesday, January 07, 2009 6:06 AM
-
| | Ling Wang Friday, January 02, 2009 12:10 PM |
|