开发者

How to maintain selected rows in DataGridView when mouse is held down on a cell?

开发者 https://www.devze.com 2023-01-06 09:55 出处:网络
I am trying to implement row moving in a DataGridView.I want to be able to select multiple rows and click on any of the selected row\'s cells to begin the drag operation.The problem is that the rows b

I am trying to implement row moving in a DataGridView. I want to be able to select multiple rows and click on any of the selected row's cells to begin the drag operation. The problem is that the rows become deselected when I hold the mouse down on a cell. How can I prevent 开发者_运维问答this from happening?


From a quick Google search, this seems to a solution for custom drag-dropping of rows. Note I just ripped the following code from the linked page, I can't vouch for its effectiveness.

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

private void dataGridView1_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 = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
        }
    }
}

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
    // Get the index of the item the mouse is below.
    rowIndexFromMouseDown = dataGridView1.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;
    }
}

private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
    // The mouse locations are relative to the screen, so they must be 
    // converted to client coordinates.
    Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
    // Get the row index of the item the mouse is below. 
    rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
    // If the drag operation was a move then remove and insert the row.
    if (e.Effect== DragDropEffects.Move)
    {
        DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
        dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
        dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
    }
}


Subclassing the datagridview helps you to do it a conditional way :

class SimpleDataGridView : DataGridView {

    public Action<DataGridViewCellMouseEventArgs> BeforeCellMouseDown;
    public Action<DataGridViewCellMouseEventArgs> AfterCellMouseDown;

    protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e) {
        if(BeforeCellMouseDown != null)
            BeforeCellMouseDown(e);

        base.OnCellMouseDown(e);

        if(AfterCellMouseDown != null)
            AfterCellMouseDown(e);
    }
}

Then, you can use it this way, in your constructor:

IEnumerable<DataGridViewRow> sel = null;

dataGridView1.BeforeCellMouseDown = 
    e => {
        if (yourCondition)
            // Save the selection
            sel = dataGridView1.SelectedRows.OfType<DataGridViewRow>();
        else
            sel = null;
    };

dataGridView1.AfterCellMouseDown = 
    e => {
        if(sel != null) {
            // Restore the selection
            foreach(var row in sel)
                row.Selected = true;
        }
    };


I'm personally not sure how to change the default behavior of the problem you're talking about, but i do know that by default a right-click doesn't do anything on the DataGridView. With that said, a work-around might be do implement something I've done before: a custom ContextMenuStrip allowing users to Copy/Paste rows by selecting the rows, right-clicking to open the context menu, copy the rows, then right-click a row header to open the context menu and paste. The CellClick and RowHeaderMouseClick events should make this easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消