ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – DataGridView多行选择清除鼠标左键拖放

2019-07-09 05:05:08  阅读:538  来源: 互联网

标签:c datagridview multi-select


我正在尝试实现Drag&在标准DataGridView上删除多行.

我按住控制键和鼠标左键选择3行.然后我释放控制键以及选择要拖动的行的内容.因此,使用鼠标左键单击并按住其中一行上的按钮,以便我可以开始拖动动作.

但是,只要我单击其中一行,就会选择该行,并取消选择其他两行,这样我就不再移动3行了.

我怎样才能解决这个问题?如果我释放鼠标左键(没有ctrl键),我只希望再次突出显示该行.这就是Windows资源管理器的工作原理.

我在Visual Studio 2010中使用C#4.0.

解决方法:

我在项目中遇到了这个问题 – 如何允许用户使用标准方法在datagridview中选择一堆行,然后将选择拖到另一个数据网格视图并删除它.我能够构建一个来自http://www.codeproject.com/Tips/338594/Drag-drop-multiple-selected-rows-of-datagridview-w的想法,它只是将DataGridView子类化为“可拖动的”DataGridView,当用户按下已经选择的行上的按钮时捕获鼠标操作,同时允许所有其他情况的标准行为:

public partial class DraggableDataGridView : System.Windows.Forms.DataGridView {

    private Rectangle dragBoxFromMouseDown;

    protected override void onm ouseDown(MouseEventArgs e) {

        // Trap the case where the user pressed the left mouse button on an already-selected row
        // without <shift> or <control>

        if ((e.Button & MouseButtons.Left) == MouseButtons.Left
            && Control.ModifierKeys == Keys.None
            && this.SelectedRows.Count > 0
            && HitTest(e.X, e.Y).RowIndex >= 0
            && this.SelectedRows.Contains(this.Rows[HitTest(e.X, e.Y).RowIndex])
            ) {

                // User pressed the mouse button over an already-selected row without <shift> or <control> so we should 
                // consider drag and drop. Record a rectangle around that mouse location to possibly trigger drag
                // operation

                Debug.WriteLine("MouseDown inside selection");
                Size dragSize = SystemInformation.DragSize;
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);

        } else {
            // In other cases use the default behavior for datagridview
            Debug.WriteLine("MouseDown outside selection");
            dragBoxFromMouseDown = Rectangle.Empty;
            base.OnMouseDown(e);
        }
    }

    protected override void onm ouseUp(MouseEventArgs e) {
        base.OnMouseUp(e);
    }

    protected override void onm ouseMove(MouseEventArgs e) {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
            // If the mouse moves outside the drag limit rectangle, start the drag.
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.Location)) {
                // Proceed with the drag and drop, passing in the selected rows
                DragDropEffects dropEffect =
                        this.DoDragDrop(this.SelectedRows, DragDropEffects.Move);
            }
        }
        base.OnMouseMove(e);
    }

    public DraggableDataGridView() {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs pe) {
        base.OnPaint(pe);
    }
}

拥有此代码,然后将此自定义组件替换为表单中的所有DataGridViews允许我将它们用作标准拖动源,并以通常的方式将它们“插入”其他组件的标准拖放功能.

标签:c,datagridview,multi-select
来源: https://codeday.me/bug/20190709/1409601.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有