C#DataGridView无法立即重新定位HScroll列。 如上所述,有什么解决方案吗?我认为这是一个BUG。 我看下面的DataGridView的源代码,我发现滚动后它不会转到“BeginColumnRelocation”。
protected virtual void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex >= this.Columns.Count)
{
throw new ArgumentOutOfRangeException("e.ColumnIndex");
}
if (e.RowIndex >= this.Rows.Count)
{
throw new ArgumentOutOfRangeException("e.RowIndex");
}
DataGridViewCell dataGridViewCell = GetCellInternal(e.ColumnIndex, e.RowIndex);
Debug.Assert(dataGridViewCell != null);
if (e.RowIndex >= 0 && dataGridViewCell.MouseMoveUnsharesRowInternal(e))
{
DataGridViewRow dataGridViewRow = this.Rows[e.RowIndex];
GetCellInternal(e.ColumnIndex, e.RowIndex).OnMouseMoveInternal(e);
}
else
{
dataGridViewCell.OnMouseMoveInternal(e);
}
DataGridViewCellMouseEventHandler eh = this.Events[EVENT_DATAGRIDVIEWCELLMOUSEMOVE] as DataGridViewCellMouseEventHandler;
if (eh != null && !this.dataGridViewOper[DATAGRIDVIEWOPER_inDispose] && !this.IsDisposed)
{
eh(this, e);
}
if (!this.dataGridViewState1[DATAGRIDVIEWSTATE1_scrolledSinceMouseDown] &&
!this.dataGridViewOper[DATAGRIDVIEWOPER_trackColResize] &&
!this.dataGridViewOper[DATAGRIDVIEWOPER_trackRowResize] &&
!this.dataGridViewOper[DATAGRIDVIEWOPER_trackColRelocation] &&
!this.dataGridViewOper[DATAGRIDVIEWOPER_trackColHeadersResize] &&
!this.dataGridViewOper[DATAGRIDVIEWOPER_trackRowHeadersResize] &&
this.AllowUserToOrderColumns &&
this.SelectionMode != DataGridViewSelectionMode.FullColumnSelect &&
this.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect &&
e.Button == MouseButtons.Left &&
this.ptMouseDownCell.Y == -1 &&
this.ptMouseDownCell.X >= 0 &&
this.ptMouseDownCell.X < this.Columns.Count)
{
Point ptGridCoord = ConvertCellToGridCoord(e.ColumnIndex, e.RowIndex, e.X, e.Y);
HitTestInfo hti = HitTest(ptGridCoord.X, ptGridCoord.Y);
Debug.Assert(hti.Type != DataGridViewHitTestType.None &&
hti.Type != DataGridViewHitTestType.HorizontalScrollBar &&
hti.Type != DataGridViewHitTestType.VerticalScrollBar);
switch (hti.typeInternal)
{
// Check for column header mouse down
case DataGridViewHitTestTypeInternal.ColumnHeader:
case DataGridViewHitTestTypeInternal.ColumnHeaderLeft:
case DataGridViewHitTestTypeInternal.ColumnHeaderRight:
case DataGridViewHitTestTypeInternal.FirstColumnHeaderLeft:
{
Debug.Assert(!this.dataGridViewState2[DATAGRIDVIEWSTATE2_messageFromEditingCtrls]);
if (Math.Abs(this.ptMouseDownGridCoord.X - ptGridCoord.X) >= DataGridView.DragSize.Width ||
Math.Abs(this.ptMouseDownGridCoord.Y - ptGridCoord.Y) >= DataGridView.DragSize.Height)
{
BeginColumnRelocation(this.ptMouseDownGridCoord.X, this.ptMouseDownCell.X);
}
break;
}
}
}
}
答案 0 :(得分:0)
我想移动列,我找到了解决方案,只需覆盖OnMouseDown并更改私有字段。
private static FieldInfo _dataGridViewState1FieldInfo = null;
protected override void OnMouseDown(MouseEventArgs e)
{
if (_dataGridViewState1FieldInfo == null)
{
var type = typeof (DataGridView);
var bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
_dataGridViewState1FieldInfo = type.GetField("dataGridViewState1", bindFlags);
}
if (_dataGridViewState1FieldInfo != null)
{
BitVector32 bits = (BitVector32) _dataGridViewState1FieldInfo.GetValue(this);
if (bits[0x00000800])
{
bits[0x00000800] = false;
_dataGridViewState1FieldInfo.SetValue(this, bits);
}
}
base.OnMouseDown(e);
}