Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# WinForm DataGridView Drag&Drop Stopped Working After Adding Dataset

$
0
0

This one's got me all confused.  I've got a datagridview (databound) that I was dragging the rows onto the form and dropping the information.  What's weird, I added a few data maintenance forms, and used the VS built in features to handle this and added the datasource and table adapters.  I changed nothing about the following code, but the drag and drop just stopped working.  I've checked the event handlers, rebuilt them, added new ones, etc.  Drag and drop stopped working as soon as I added the dataset to my project.  The grid is easy enough, i have a class that pulls unscheduled info and the grid connects to the outgoing table.  And what's weirder, the grid is set to "Public" in the modifiers, but I've got 3 inherited forms from the base, and it's only editable in the very first form, events and properties are grayed out after that.  So I can't just stick it in the code later on to see if it works because I'm locked out. 

Some hunting around got me this code chunk (I'd post the source but I can't remember) that does the drag and drop.  It doesn't even activate the "dragging" effects with the little box anymore.  Sorry if this belongs under "Form Controls" page or something, but this seems to be the control and the code elsewhere, just hoping for some insight if anyone else has ran into this.

private Rectangle dragBoxFromMouseDown;
        private int rowIndexFromMouseDown;
        private int rowIndexOfItemUnderMouseToDrop;

        private void unscheduledGrid1_MouseMove(object sender, MouseEventArgs e)
        {

            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                // If the mouse moves outside the rectangle, start the drag.
                if (dragBoxFromMouseDown != Rectangle.Empty &&
                    !dragBoxFromMouseDown.Contains(e.X, e.Y))
                {

                    // Proceed with the drag and drop, passing in the list item.
                    DragDropEffects dropEffect = unscheduledGrid1.DoDragDrop(
                    unscheduledGrid1.Rows[rowIndexFromMouseDown],
                    DragDropEffects.Move);


                }
            }
        }

        private void unscheduledGrid1_MouseDown(object sender, MouseEventArgs e)
        {
            rowIndexFromMouseDown = unscheduledGrid1.HitTest(e.X, e.Y).RowIndex;
            if (rowIndexFromMouseDown != -1)
            {
                // Remember the point where the mouse down occurred.
                // The DragSize indicates the size that the mouse can move
                // before a drag event should be started.
                Size dragSize = SystemInformation.DragSize;

                // Create a rectangle using the DragSize, with the mouse position being
                // at the center of the rectangle.
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                               e.Y - (dragSize.Height / 2)),
                                    dragSize);
            }
            else
                // Reset the rectangle if the mouse is not over an item in the ListBox.
                dragBoxFromMouseDown = Rectangle.Empty;

        }


May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>