0

我正在尝试将拖放功能实现到 DevExpress 网格中。我尝试的第一种方法不起作用,因为该dragdrop方法没有受到影响。我是否必须让它由mouseDown事件触发,或者我该如何去做。示例将不胜感激。到目前为止,我尝试的是:

private void gridFields_DragDrop(object sender, DragEventArgs e)
{
    GridControl grid = sender as GridControl;
    GridView view = grid.MainView as GridView;
    GridHitInfo srcHitInfo = e.Data.GetData(typeof(GridHitInfo)) as GridHitInfo;
    GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
    int sourceRow = srcHitInfo.RowHandle;
    int targetRow = hitInfo.RowHandle;
    MoveRow(sourceRow, targetRow);
}

private void MoveRow(int sourceRow, int targetRow)
{
    if (sourceRow == targetRow || sourceRow == targetRow + 1)
        return;

    GridView view = gridFieldView;
    DataRow row1 = view.GetDataRow(targetRow);
    DataRow row2 = view.GetDataRow(targetRow + 1);
    DataRow dragRow = view.GetDataRow(sourceRow);
    decimal val1 = (decimal)row1[OrderFieldName];
    if (row2 == null)
        dragRow[OrderFieldName] = val1 + 1;
    else
    {
        decimal val2 = (decimal)row2[OrderFieldName];
        dragRow[OrderFieldName] = (val1 + val2) / 2;
    }
}
4

2 回答 2

2

抱歉,我想将其添加为评论,但不允许我这样做。

我假设您希望允许用户通过拖动来在 [WinForms] 网格中上下移动行。您实际上认为需要设置 MouseDown 事件是正确的。此外,您还需要 MouseMove 事件。

试试这个指南!http://tv.devexpress.com/Content/XtraGrid/XtraGridDragRowsBetweenGrids/XtraGridDragRowsBetweenGrids.pdf

如果您无法访问它,请告诉我。

更新

试试这些。它们是针对我的需求的,但我认为你应该能够重新利用它们。

    /// <summary>
    /// Drag n drop mouse down
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Grid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        GridView view = sender as GridView;
        downHitInfo = null;

        GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
        if (Control.ModifierKeys != Keys.None) return;
        if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.ColumnEdge
            && hitInfo.HitTest != GridHitTest.RowDetailEdge && hitInfo.HitTest != GridHitTest.RowEdge)
        {
            downHitInfo = hitInfo;
        }
    }

    /// <summary>
    /// Drag n drop mouse move, kicks off DragDrop
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Grid_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        GridView view = sender as GridView;
        if (e.Button == MouseButtons.Left && downHitInfo != null)
        {
            Size dragSize = SystemInformation.DragSize;
            Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                this.gridControl.DoDragDrop(downHitInfo, DragDropEffects.All);
                downHitInfo = null;
            }
        }
    }

希望有帮助。

于 2012-12-14T18:32:33.707 回答
0
GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));

你调用的对象是空的。

于 2014-06-19T09:04:59.030 回答