我正在尝试为标准DataGridView实现多行拖放。
我按住Ctrl键并用左键选择3行。然后我释放Ctrl键并希望选择要拖动的行。因此,我使用左键单击并按住其中一行上的按钮,以便开始拖动。
但是,一旦我单击其中一行,该行就会被选中,其他2行就会被取消选择,因此我不再移动3行。
如何解决这个问题?我只希望在释放左键(没有按下Ctrl键)时才重新突出显示该行。这就是Windows资源管理器的工作方式。
我正在使用Visual Studio 2010中的C#4.0。
我正在尝试为标准DataGridView实现多行拖放。
我按住Ctrl键并用左键选择3行。然后我释放Ctrl键并希望选择要拖动的行。因此,我使用左键单击并按住其中一行上的按钮,以便开始拖动。
但是,一旦我单击其中一行,该行就会被选中,其他2行就会被取消选择,因此我不再移动3行。
如何解决这个问题?我只希望在释放左键(没有按下Ctrl键)时才重新突出显示该行。这就是Windows资源管理器的工作方式。
我正在使用Visual Studio 2010中的C#4.0。
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);
}
}
我没有更简单的解决方案,但这应该可以工作。整个想法是将所选行的背景颜色更改为SelectionBackColor
,将所选行的前景颜色更改为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
...
我希望这可以帮助到你,也希望有人能提供更好的解决方案!