Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在单元格上按住鼠标时,如何维护DataGridView中的选定行?_C#_Winforms_Datagridview_Rows_Selected - Fatal编程技术网

C# 在单元格上按住鼠标时,如何维护DataGridView中的选定行?

C# 在单元格上按住鼠标时,如何维护DataGridView中的选定行?,c#,winforms,datagridview,rows,selected,C#,Winforms,Datagridview,Rows,Selected,我试图在DataGridView中实现行移动。我希望能够选择多行并单击所选行的任意单元格以开始拖动操作。问题是,当我在单元格上按住鼠标时,行被取消选中。如何防止这种情况发生?我个人不确定如何更改您所讨论的问题的默认行为,但我知道默认情况下,右键单击在DataGridView上不起任何作用。话虽如此,一个解决办法可能是实现我以前做过的事情:自定义ContextMenuStrip,允许用户通过选择行、右键单击打开上下文菜单、复制行,然后右键单击行标题打开上下文菜单并粘贴来复制/粘贴行。 CellCl

我试图在DataGridView中实现行移动。我希望能够选择多行并单击所选行的任意单元格以开始拖动操作。问题是,当我在单元格上按住鼠标时,行被取消选中。如何防止这种情况发生?

我个人不确定如何更改您所讨论的问题的默认行为,但我知道默认情况下,右键单击在DataGridView上不起任何作用。话虽如此,一个解决办法可能是实现我以前做过的事情:自定义ContextMenuStrip,允许用户通过选择行、右键单击打开上下文菜单、复制行,然后右键单击行标题打开上下文菜单并粘贴来复制/粘贴行。
CellClick和RowHeaderMouseClick事件应该会使这变得更容易。

通过快速的谷歌搜索,似乎是一种自定义行拖放的解决方案。注意,我刚刚从链接页面中删除了以下代码,我不能保证它的有效性

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);
    }
}

对datagridview进行子类化有助于以有条件的方式执行此操作:

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);
    }
}
类SimpleDataGridView:DataGridView{
在Cellmousedown之前的公共行动;
细胞下移后的公共行动;
CellMouseDown(DataGridViewCellMouseEventArgs e)上的受保护覆盖无效{
if(BeforeCellMouseDown!=null)
在Cellmousedown(e)之前;
碱基。OnCellMouseDown(e);
if(AfterCellMouseDown!=null)
细胞后向下(e);
}
}
然后,您可以在构造函数中以这种方式使用它:

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;
        }
    };
IEnumerable sel=null;
dataGridView1.BeforeCellMouseDown=
e=>{
如果(你的情况)
//保存所选内容
sel=dataGridView1.SelectedRows.OfType();
其他的
sel=null;
};
dataGridView1.AfterCellMouseDown=
e=>{
如果(sel!=null){
//恢复所选内容
foreach(sel中的var行)
row.Selected=true;
}
};

我认为这并不能解决问题:“问题是当我在单元格上按住鼠标时,行被取消选择。”我也在寻找解决这个问题的方法。