我正在尝试实施Drag&在标准DataGridView上删除多行。
我按住控制键和鼠标左键选择3行。然后我释放控制键以及选择要拖动的行的内容。因此,使用鼠标左键单击并按住其中一行上的按钮,以便我可以开始拖动动作。
但是,只要我单击其中一行,就会选中该行,取消选择其他两行,这样我就不再移动3行了。
如何解决这个问题?如果我释放鼠标左键(没有ctrl键),我只希望再次突出显示该行。这就是Windows资源管理器的工作原理。
我在Visual Studio 2010中使用C#4.0。
答案 0 :(得分: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,允许我将它们用作标准拖动源,并以常规方式将它们“插入”其他组件的标准drop函数。
答案 1 :(得分: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;
}
}
}
我希望您了解自定义和自行编码的整个想法,例如,如果dataGridView失去焦点,则还原BackColor
和ForeColor
...
我希望它有所帮助,有人可以提供更好的解决方案!