简体   繁体   English

在DevExpress网格中拖放

[英]Drag and drop in DevExpress grid

I'm trying to implement a drag and drop feature into a DevExpress Grid. 我正在尝试将拖放功能实现到DevExpress网格中。 The first way I tried it didn't work, as the dragdrop method isn't getting hit. 我尝试它的第一种方法不起作用,因为dragdrop方法没有被击中。 Do I have to have it triggered by the mouseDown event, or how do I go about doing this. 我是否必须由mouseDown事件触发它,或者我该如何执行此操作。 Examples would be appreciated. 例子将不胜感激。 What I tried so far is this: 到目前为止我尝试的是:

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;
    }
}

Sorry, I wanted to add this as just a comment, but SO won't allow me. 对不起,我想把它添加为评论,但是SO不允许我这样做。

I assume that you want to allow the user to move rows up and down the [WinForms] grid by dragging them. 我假设您希望允许用户通过拖动来在[WinForms]网格中上下移动行。 You are actually correct in thinking you need to set up the MouseDown event. 您认为需要设置MouseDown事件实际上是正确的。 In addition, you'll need the MouseMove event. 此外,您还需要MouseMove事件。

Try this guide! 试试这本指南! http://tv.devexpress.com/Content/XtraGrid/XtraGridDragRowsBetweenGrids/XtraGridDragRowsBetweenGrids.pdf http://tv.devexpress.com/Content/XtraGrid/XtraGridDragRowsBetweenGrids/XtraGridDragRowsBetweenGrids.pdf

If you can't access it, let me know. 如果您无法访问它,请告诉我。

Update 更新

Try these. 试试这些。 They were specific to my needs but I think you should be able to re-purpose them. 它们特定于我的需求,但我认为你应该能够重新定位它们。

    /// <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;
            }
        }
    }

Hope that helps. 希望有所帮助。

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

你调用的对象是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM