Please help with Drag and Drop

Last post 11-05-2008 6:52 PM by sachintha81. 19 replies.
Page 2 of 2 (20 items) < Previous 1 2
Sort Posts: Previous Next
  • 11-03-2008 10:43 PM In reply to

    Re: Please help with Drag and Drop

    I did post the code above. Here it is again:

     

    private void dgvSchedule_DragDrop(object sender, DragEventArgs e)
    {
                DataGridView.HitTestInfo htiSchedule = dgvSchedule.HitTest(e.X, e.Y);
                dgvSchedule.Rows[htiSchedule.RowIndex].Cells[htiSchedule.ColumnIndex].Value = e.Data.GetData(DataFormats.Text);

    }

     

    Here, you create an instance "htiSchedule" and get the result of the HitTest() methoud of the DGV to it.

    So now htiSchedule contains the information such as the X and Y coordinates of the mouse where it hit the DGV wehn clcking - in turn the cell. So it is quite useful and solved my problem.

  •  Advertisement

    3 MONTHS FREE & FREE SETUP on ASP.NET 3.5/2.0 Web Hosting! Windows 2008 & 2003 Servers Available, MS SQL 2008/2005, .NET 3.5 SP1, Entity Framework, LINQ, Silverlight 2.0, 30 Day Money Back Guarantee – Click Here!

     
  • 11-04-2008 11:50 AM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Please help with Drag and Drop

     No, the whole thing. Do you mind?

    Or do you want explore other alternatives such as sys.preview?

    here is a nice tutorial and sample,

    http://msdn.microsoft.com/en-us/magazine/cc135985.aspx

    please let me know

  • 11-04-2008 6:59 PM In reply to

    Re: Please help with Drag and Drop

    OK, here we go.

     

    //▼--------------------------------Schedule DGV------------------------------------▼//

            private void dgvSchedule_DragEnter(object sender, DragEventArgs e)
            {
                bDragList = true;

                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }

            private void dgvSchedule_DragDrop(object sender, DragEventArgs e)
            {
                Point pt = dgvSchedule.PointToClient(new Point(e.X, e.Y));
                htiSchedule = dgvSchedule.HitTest(pt.X, pt.Y);

                if ((htiSchedule.ColumnIndex == 0) && (bDragSchedule == true))
                {
                    MessageBox.Show("CANT COPY DATA HERE");
                    bDragSchedule = false;
                }
                else
                {
                    string str = (string)dgvSchedule.Rows[htiSchedule.RowIndex].Cells[htiSchedule.ColumnIndex].Value;

                    if ((str != null) && (bDragSchedule == true))
                    {
                        MessageBox.Show("DATA ALREADY IN");
                        bDragSchedule = false;
                    }
                    else if ((str == null) && (bDragSchedule == true))
                    {
                        dgvSchedule.Rows[htiSchedule.RowIndex].Cells[htiSchedule.ColumnIndex].Value = e.Data.GetData(DataFormats.Text);
                        bDataDrop = true;
                        bDragSchedule = false;
                    }
                }
            }

            private void dgvSchedule_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    DataGridView.HitTestInfo info = dgvSchedule.HitTest(e.X, e.Y);

                    if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                    {
                        string text = (String)dgvSchedule.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;

                        if (text != null)
                        {
                            dgvSchedule.DoDragDrop(text, DragDropEffects.Copy);
                            dgvSchedule.ClearSelection();
                            if (bDataDrop == true)
                            {
                                dgvSchedule.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = null;
                                dgvSchedule.ClearSelection();
                                bDataDrop = false;
                            }
                        }
                    }
                }
            }

            private void dgvSchedule_DragOver(object sender, DragEventArgs e)
            {
                bDragSchedule = true;
            }

            //▲-----------------------------End of DGV Schedule-------------------------------------▲//



            //▼-----------------------------Data List DGV------------------------------------▼//

            private void dgvItemList_DragEnter(object sender, DragEventArgs e)
            {
                bDragSchedule = true;

                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }


            private void dgvItemList_DragDrop(object sender, DragEventArgs e)
            {
                if (bDragList == true)
                {
                    dgvItemList.Rows.Add(1);

                    int iLastRowIndex = dgvItemList.RowCount;

                    dgvItemList.Rows[iLastRowIndex - 1].Cells[0].Value = e.Data.GetData(DataFormats.Text);

                    bDataDrop = true;
                    bDragList = false;
                }
            }


            private void dgvItemList_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    DataGridView.HitTestInfo info = dgvItemList.HitTest(e.X, e.Y);

                    if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                    {
                        string text = (String)dgvItemList.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;

                        if (text != null)
                        {
                            dgvItemList.DoDragDrop(text, DragDropEffects.Copy);
                            if (bDataDrop == true)
                            {
                                dgvItemList.Rows.RemoveAt(info.RowIndex);
                                bDataDrop = false;
                            }
                        }
                    }
                }
            }

            //▲----------------------------End of DGV List-------------------------------------------------▲//

     

    Lemme know if there are any questions.

    Cheers!

  • 11-05-2008 4:41 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Please help with Drag and Drop

     hey, I found a perfect example for you (that is my way of learning new things. Find a working example then go from there).

    http://www.codeproject.com/KB/cpp/DataGridView_Drag-n-Drop.aspx

    Let me know if that helps.

     

  • 11-05-2008 6:52 PM In reply to

    Re: Please help with Drag and Drop

    Thanks mate!

    Actually I did come across that and if you look at my code, it's pretty much the same!

    Thanks anywayz man.

Page 2 of 2 (20 items) < Previous 1 2