2013-06-03 39 views
4

我试图实现拖动&删除标准DataGridView上的多行。DataGridView多行选择清除鼠标左键拖放

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

但是当我点击行之一,该行被选中,其他2行被取消选择,所以我不再移动3行。

我该如何解决这个问题?我只希望如果我释放鼠标左键(没有Ctrl键),该行将再次突出显示。这是Windows资源管理器的工作原理。

我使用C#4.0在Visual Studio 2010

回答

0

我没有一个更简单的解决方案,但这个应该工作。整个想法是将所选行的BackColor更改为SelectionBackColor,并将选定行的ForeColor更改为SelectionForeColor。他们看起来像被选中。我想DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect很容易得到SelectedRows,而不是通过点击行标题来选择行。我还假设Form.KeyPreview = true

这里是我的代码:

//This is to copy the SelectedRows every time user releases the Control key 
DataGridViewRow[] selectedRows; 
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e){ 
    if(selectedRows == null) return; 
    if(!selectedRows.Contains(dataGridView.Rows[e.RowIndex])){ 
     //Restore the BackColor and ForeColor of the selected rows 
     foreach(DataGridViewRow row in selectedRows){ 
      row.DefaultCellStyle.BackColor = dataGridView.DefaultCellStyle.BackColor; 
      row.DefaultCellStyle.ForeColor = dataGridView.DefaultCellStyle.ForeColor; 
     } 
    } 
} 
private void form_KeyUp(object sender, KeyEventArgs e){ 
    if(e.KeyCode == Keys.ControlKey){ 
    selectedRows = new DataGridViewRow[dataGridView.SelectedRows.Count]; 
    dataGridView.SelectedRows.CopyTo(selectedRows,0); 
    foreach(DataGridViewRow row in selectedRows){ 
     row.DefaultCellStyle.BackColor = dataGridView.DefaultCellStyle.SelectionBackColor; 
     row.DefaultCellStyle.ForeColor = dataGridView.DefaultCellStyle.SelectionForeColor; 
    } 
    } 
} 

我希望你明白的整体思路进行定制和自己的代码,如恢复BackColorForeColor如果在DataGridView失去焦点..

我希望它有帮助,有人可以提供更好的解决方案!

1

我面临的一个项目这个确切的问题 - 如何让用户使用标准方法在一个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 OnMouseDown(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 OnMouseUp(MouseEventArgs e) { 
     base.OnMouseUp(e); 
    } 

    protected override void OnMouseMove(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允许我使用这些作为标准拖动源,而“塞”他们到了在平时的其他组件的标准降功能办法。

+0

另一种味道是http://stackoverflow.com/questions/1942636/how-do-i-mimic-windows-explorer-multi-select-drag-n-drop-behavior-in-a-datagridv – onupdatecascade