【问题标题】:Drag and Drop rows and separate option to Drag and drop Cells in C#拖放行和单独的选项以在 C# 中拖放单元格
【发布时间】:2019-06-21 05:33:59
【问题描述】:

我有一个空的 Datagridview。 用户能够将值从列表拖放到复制文本的 Datagridview 中。用户还能够在移动文本的 Datagrid 视图中拖放值。

但我还希望行能够拖放(更改它们出现的顺序)。

我设法通过使用以下 2 个答案提供的代码分别完成这两项工作:

https://stackoverflow.com/a/21133200/10086705(用于单元到单元) How could I Drag and Drop DataGridView Rows under each other?(用于拖动行)。

问题是它们都使用相同的事件,我当前的解决方案是使用复选框来查看要使用的事件。 (检查行,未检查单元格)这很有效,我不相信这是最有效/用户友好的方式。

这是拖动单元格的代码。 (不要介意尝试捕获它们是临时解决方案。)

    private Rectangle dragBoxFromMouseDown;
    private object valueFromMouseDown;
    private DataGridViewCell origin;
    private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {                    
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
                try
                {
                    DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
                }
                catch{}
            }
        }
    }
    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        origin = sender as DataGridViewCell;
        var hittestInfo = dataGridView1.HitTest(e.X, e.Y);

        if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
        {
            valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value;
            if (valueFromMouseDown != null)
            {
                origin = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex] as DataGridViewCell;         
                Size dragSize = SystemInformation.DragSize;
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
            }
        }
        else
        {
            dragBoxFromMouseDown = Rectangle.Empty;
        }
    }
    private void dataGridView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));

        if (e.Effect == DragDropEffects.Copy)
        {
            string cellvalue = e.Data.GetData(typeof(string)) as string;
            var hittest = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
            if (hittest.ColumnIndex != -1 && hittest.RowIndex != -1)
            {
                try
                {
                    if (dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value.ToString() != "")
                    {
                        DialogResult dialogResult = MessageBox.Show("Are you sure you want to replace this value?", "!", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.Yes)
                        {
                            dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
                            try{origin.Value = "";}catch{}
                        }
                        else if (dialogResult == DialogResult.No){}
                    }
                }
                catch
                {
                    dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;

                    try{origin.Value = "";}catch{}
                }
            }
        }
    }

我希望的是一个 IF 语句检查是否已选择行单元格或是否已选择 RowHeader 的可能性。如果是单元格,它应该只将文本从一个单元格移动到另一个单元格,而如果选择 RowHeader,它应该将该行移动到新位置(不覆盖任何现有行)

【问题讨论】:

    标签: c# datagridview drag-and-drop


    【解决方案1】:

    解决了,“if (dataGridView1.SelectedRows.Count == 1)”就行了。如果这是真的,它将移动行而不是单元格。从以下网站获得: https://www.codeproject.com/Articles/811035/Drag-and-Move-rows-in-DataGridView-Control

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多