在DevExpress网格中拖放

时间:2012-12-14 17:23:03

标签: c# drag-and-drop grid devexpress

我正在尝试将拖放功能实现到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;
    }
}

2 个答案:

答案 0 :(得分: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;
            }
        }
    }

希望有所帮助。

答案 1 :(得分:0)

GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));

对象引用未设置为对象的实例。