拖放Datagrid视图Winform C#行 [英] Drag and Drop Rows of Datagrid View Winform C#

查看:61
本文介绍了拖放Datagrid视图Winform C#行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一网格视图中将行从特定位置拖动到另一个位置。其他行应根据拖放自动调整。

I want to Drag Rows from certain position to another position in same grid view. The Other rows should automatically adjust according to the Drag and Drop.

谢谢

推荐答案

在应用程序中执行d& d时,我更喜欢使用鼠标事件而不是实际的拖放事件。

I much prefer using mouse events over actual drag&drop events for this when doing d&d within an application.

1-未绑定示例

以下是使用鼠标的简单示例事件,同时在 Label 中显示一个 Cell 值时拖动行。

Here is a simple example that uses the mouse events to drag rows around while diplaying one Cell value in a Label.

它将行插入到您插入的行之后。

It inserts the row after the one you drop it on.

dragLabel 部分是可选的..

首先我们声明两个帮助变量在课程级别:

First we declare two helper variables at class level:

int dragRow = -1;
Label dragLabel = null;

然后我们对 CellMouseDown 进行编码标签 ..:

Then we code the CellMouseDown to prepare the Label..:

private void dataGridView1_CellMouseDown(object sender,DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex<0 || e.RowIndex < 0) return;
    dragRow = e.RowIndex;
    if (dragLabel == null) dragLabel = new Label();
    dragLabel.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
    dragLabel.Parent = dataGridView1;
    dragLabel.Location = e.Location;
}

MouseMove 我们只是移动标签。但是,由于 MouseDown 通常会开始一个单元格选择,因此我们会清除选择。.

In the MouseMove we just move the Label. But as the MouseDown usually starts a cell selection we clear the selection..

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && dragLabel != null)
    {
        dragLabel.Location = e.Location;
        dataGridView1.ClearSelection();
    }
}

真正的工作在 MouseUp 。我们需要一个 HitTest 来找出我们所在的行(如果有的话)。我们还需要根据拖动的方向来计算目标行。

The real work is in the MouseUp. We need a HitTest to find out which, if any, row we are on. We also need to calculate the target row depending on the direction of the drag.

最后,我们清理 Label ..

Finally we clean up the Label..

private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
{
    var hit = dataGridView1.HitTest(e.X,e.Y);
    int dropRow = -1;
    if (hit.Type != DataGridViewHitTestType.None)
    {
        dropRow =   hit.RowIndex;
        if (dragRow >= 0 )
        {
            int tgtRow = dropRow + (dragRow > dropRow ? 1 : 0);
            if (tgtRow != dragRow)
            {
                DataGridViewRow row = dataGridView1.Rows[dragRow];
                dataGridView1.Rows.Remove(row);
                dataGridView1.Rows.Insert(tgtRow, row);

                dataGridView1.ClearSelection();
                row.Selected = true;
            }
        }
    }
    else  { dataGridView1.Rows[dragRow].Selected = true;}

    if (dragLabel != null)
    {
        dragLabel.Dispose();
        dragLabel = null;
    }
}

一些注意事项:

请确保没有新的行或未修改的行,或者您无法移动它。我花了一段时间才意识到,在最初添加行之前,我必须关闭 AllowUserToAddRows

Make sure no row is new or modified or you can't move it. It took me awhile to realize that I had to switch off AllowUserToAddRows before adding the rows initially..

选择取决于您。如果放置失败,我选择选择拖动的行。我选择让它拖到空白处时失败。

The treatment of selections is up to you. I chose to select the dragged row if the drop fails. And I chose to let it fail when dragging into the empty space.

更新:

2-DataBound示例

如果DGV是数据绑定,则不能移动行直。相反,您需要移动 DataSource 中的行。

If the DGV is data-bound you can't move the rows directly. Instead you need to move the rows in the DataSource.

让我们假设您有一个数据表DT 。然后,您可以从中删除行并在任何位置插入行。 (与SQL DBMS表完全不同!)

Let's assume you have a DataTable DT. Then you can remove rows from it and insert rows at any position. (Quite different from SQL DBMS tables!)

请注意, DataRow 一旦从<$中删除,其值就会丢失。 c $ c> DataTable 。因此,我们需要在删除行之前克隆值。除此之外,代码几乎相同。只需用以下内容替换最里面的条件:

Note that a DataRow loses its values once removed from the DataTable. Therefore we need to clone the values before we remove the row. Other than that the code is pretty much the same. Simply replace the innermost condition with this:

if (tgtRow != dragRow)
{
    DataRow dtRow = DT.Rows[dragRow];
    DataRow newRow = DT.NewRow();
    newRow.ItemArray = DT.Rows[dragRow].ItemArray; // we need to clone the values

    DT.Rows.Remove(dtRow);
    DT.Rows.InsertAt(newRow, tgtRow);
    dataGridView1.Refresh();
    dataGridView1.Rows[tgtRow].Selected = true;
}

这篇关于拖放Datagrid视图Winform C#行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆