Please help with Drag and Drop

Last post 11-05-2008 6:52 PM by sachintha81. 19 replies.
Page 1 of 2 (20 items) 1 2 Next >
Sort Posts: Previous Next
  • 10-21-2008 3:35 AM

    Please help with Drag and Drop

    This is my requirement.

    I am allowed to use a DGV (or a List View or some other control that can be used in tabular form).

    Then, I have a set of labels/text boxes that are created in runtime according to some data from a DB. Let's say just text tabels.

    So I need to drag and drop contents of these run time generated textboxes/labels onto any preferred cell in the DGV (or that any other control). When that is done, I need to be able to delete/destroy the original label as well.

    On the other hand, if the user decides to take some data from the DGV and put them back in the original labels, that should also be possible. SO I guess those labels/textboxes need to be put inside of some kind of a control.

    But I'm relatively new to C#, so haven't got much of an idea so as to how to do this. I could get text on labels to be copied to another lables, but when I used a DGV I got stuck.
    Main reason being, when an item is dragged onto the DGV, there seems to be no event to detect on which cell the mouse is. The Mouse_Enter method works only for simple mouse enter movements - not when dragging.

    PLEASE HELP!

  •  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!

     
  • 10-21-2008 10:43 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

     Drag and drop has much more to do with JavaScript and other client side script variants (such as JQuery). There are many custom-built drag and drop libraries out there. I would say, C# cannot help us with drag-n-drop in any way at all.

    Your problem is little more difficult than regular drag-n-drop, because it has to remember each cell's coordinates to know where the mouse action ends.

    This article should give you some ideas.

    http://www.isocra.com/2007/07/dragging-and-dropping-table-rows-in-javascript/

     

  • 10-21-2008 8:10 PM In reply to

    Re: Please help with Drag and Drop

    Thank you a lot.

    Yes that exactly is the problem.

    If it is not a drag-and-drop but a simple mouse movement then I can detect on which cell the mouse is by simply using 'cellMouseEnter' event. But when the mouse is dragging and entering the DGV, there seems to be no event to handle that. Thus I'm facing this problem.

    So as the article suggest I have to use the actual coordinates of the mouse? Doesn't really look like appealing.    :(

  • 10-21-2008 8:55 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

    you are right. We should not have to deal with such raw details.

    this is another example pretty similar to your scenarios,  using asp .net ajax preview,

    http://aspalliance.com/1300_Custom_Client_Side_Drag_and_Drop_Behavior_in_ASPNET_AJAX.all#Page4

    If you still need more code sample I will try to work out something for you in a couple of days. 

  • 10-21-2008 9:15 PM In reply to

    Re: Please help with Drag and Drop

    Thanks mate!


    Please do give me a code if possible, because I'm sort of alien to ajax and asp.net.

    Meantime, I've started to work on the above mentioned mouse position method, though it seems tiresome and inefficient.

    Cheers!

  • 10-21-2008 9:21 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

     no, please try to learn from the second example instead. I agree with you, dealing with mouse coordinates is too low-level, given there are so many good drag-n-drop examples and libraries

  • 10-21-2008 9:26 PM In reply to

    Re: Please help with Drag and Drop

    Oh OK then. I will try my best to do it somehow. Besides my PM wants it ASAP!  :Big Smile

  • 10-28-2008 1:09 AM In reply to

    Re: Please help with Drag and Drop

    Hello, OK got it done using the hitTest method. Thanks.

    But there is some other issue I want to get solved.

    In my C# Application program, I have two DataGridViews which are not bound to any data source. I have coded them so that the user is allowed to drag and drop data between the two DGVs as well as within them.
    So, when a data item in a cell is dragged and dropped on another cell, the data on the original is deleted - that is set to null.
    The problem is, when I do that, after dropping, it automatically select the last cell (the one in which I just set the value to null).
    How do I get that cell to be NOT selected?

    Here is my code:


    dgvSchedule.DoDragDrop(text, DragDropEffects.Copy);
    if (bDataDrop == true)
    {
               dgvSchedule.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = null;         //setting the value to null
               dgvSchedule.Rows[info.RowIndex].Cells[info.ColumnIndex].Selected = false;   //setting that cell to not selected
               bDataDrop = false;               
    }



    There, right after I set the value to null, I set the 'selected' property to false, but despite that it still selects that.

    Please HELP!

  • 10-29-2008 11:56 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

     it is great you figured out. However i have no idea what you are talking about. Did you use sys.preview? what is the hittest method? sorry more questions

  • 10-29-2008 7:55 PM In reply to

    Re: Please help with Drag and Drop

    Hi xxxd, thanks!

    Yeah, got it done. And HitTest is quite useful it seems. This is the bit of code I used.

     

    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, in DragDrop event, I create htiSchedule which contains all the information of where the mouse 'hit' the DGV.

    So it contains the column and row the mouse touched, hence I solved my problem.

     

    But, I now have some other problem, with selecting (not selecting, actually) a cell as described in the above post. Please do help with that if you can.

     

    Cheers!

  • 10-29-2008 8:32 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

     wow, yet something new again. I just did my version of drag'n'drop with sys.preview. Tomorrow I will take a look at this Hittest. Will let you know if I can figure out anything

     

  • 10-29-2008 8:37 PM In reply to

    Re: Please help with Drag and Drop

    Thanks!

    There is certainley something wrong with my selection of a cell though.
    After I drag and drop an item, the selection ALWAYS jumps to that previous cell. I can't get it to change.

    Wonder what the reason...

  • 10-30-2008 12:56 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

    I am sorry, you certainly know more than I do. Is Hittest flash action script? I searched a bit, that is what i got? 

  • 10-30-2008 8:19 PM In reply to

    Re: Please help with Drag and Drop

    As far as I can remember, HitTest can be found in Flash AS too, but isn't it a bit different? I mean, I think it can be used to detect the collision of two MovieClips too, can't it? I'm not sure, it's been ages since I did any AS.

    Anyway, HitTest is in Visual C# too, and It is quite useful, cos it contains all the details where the mouse hit the DGV.

  • 10-31-2008 9:02 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

     can you post the code? I cannot promise I will solve the problem, as you know I am at ground zero right now, but I will promise to learn and do my best to help.

    A few days ago I indeed have cracked out a js version using sys.preview

Page 1 of 2 (20 items) 1 2 Next >