【问题标题】:Unable to drag and drop text from DataGridView to TextBox C#无法将文本从 DataGridView 拖放到 TextBox C#
【发布时间】:2015-02-19 02:28:39
【问题描述】:

我一直在使用其他人在线提供的代码,但由于某种原因,它不允许我将项目从 datagridview 拖动到文本框。我突出显示 dataGridView 中的一行并尝试将其拖到文本框,但没有任何反应。我还为 textBox 启用了 drop 属性,但仍然没有区别。这是我正在使用的代码:

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
         if (e.Button == MouseButtons.Left)
         {
             DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
             if (info.RowIndex >= 0)
             {
                 if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                 {
                     string text = (String)
                      dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
                     if (text != null)
                         dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
                 }
             }
         }
    }

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(System.String)))
        {
            textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
        }
    }

    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

【问题讨论】:

  • 这是在桌面应用还是网络应用上?

标签: c# datagridview drag-and-drop


【解决方案1】:

这是我所做的一个小示例,旨在让您了解如何执行此操作……非常适合我。我在这里使用了 WinForms。如果是 WPF,您可能需要注册更多事件才能注册拖放操作...

请注意,当您将一个项目从一个控件拖到另一个控件时,您需要在各处添加更多代码来执行您真正想做的事情。

public partial class Form1 : Form
{
    private Rectangle dragBoxFromMouseDown;
    private int rowIndexFromMouseDown;
    private int rowIndexOfItemUnderMouseToDrop;
    private DataGridViewRow draggedrow;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<StringValue> Items = new List<StringValue>() { new StringValue("1"), new StringValue("2"), new StringValue("3"), new StringValue("4"), new StringValue("5"), new StringValue("6") };
        this.dataGridView1.DataSource = Items;

    }



    private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // If the mouse moves outside the rectangle, start the drag.
            if (dragBoxFromMouseDown != Rectangle.Empty &&
                !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {

                // Proceed with the drag and drop, passing in the list item.                    
                DragDropEffects dropEffect = dataGridView1.DoDragDrop(
                dataGridView1.Rows[rowIndexFromMouseDown],
                DragDropEffects.Move);
            }
        }
    }

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the index of the item the mouse is below.
        rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
        if (rowIndexFromMouseDown != -1)
        {
            // Remember the point where the mouse down occurred. 
            // The DragSize indicates the size that the mouse can move 
            // before a drag event should be started.                
            Size dragSize = SystemInformation.DragSize;

            // Create a rectangle using the DragSize, with the mouse position being
            // at the center of the rectangle.
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                           e.Y - (dragSize.Height / 2)),
                                dragSize);
            this.draggedrow = this.dataGridView1.CurrentRow;
        }
        else
            // Reset the rectangle if the mouse is not over an item in the ListBox.
            dragBoxFromMouseDown = Rectangle.Empty;
    }

    private void dataGridView1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        this.textBox1.Text = (string)this.draggedrow.Cells["Value"].Value;
    }


}

public class StringValue
{
    public StringValue(string s)
    {
        _value = s;
    }
    public string Value { get { return _value; } set { _value = value; } }
    string _value;
}

【讨论】:

  • 我刚刚尝试了该代码,但仍然和以前一样:/我是否需要更改 datagridview 或文本框的属性?我已经启用了文本框属性下的下拉选项
  • 我的都设置为 AllowDrop。而已。您是否也正确订阅了所有活动?只是粘贴我的代码不会将你的对象事件绑定到正确的代码......
  • 是的,我调整了您的代码以与我拥有的表单组件兼容。我还将 textbox 和 datagridview 属性都设置为 AllowDrop 并且仍然没有任何区别:(
  • 这很奇怪。这对我很有效。这是 WinForms 还是 WPF 项目?
  • 这正是我所做的... 1:创建新的 winforms 项目 2:在 UI 中双击 form1 3:通过替换当前代码来复制粘贴我的代码 } 4:将 DataGridView 放在form1 5:在 form1 上放置 TextBox 6:选择 GridView 并订阅您复制粘贴的事件 7:选择 TextBox 并订阅您复制粘贴的事件 8:将 GridView 和 TextBox 设置为 AllowDrop = true 9:运行 10:选择并拖动从 gridview 到文本框的整个 ROW...完成。
【解决方案2】:

你不能使用 DataGridViewCellMouseEventArgs e 而不是 hittest 来获取 dataGridView1_CellMouseDown 中的行索引。以下是您修改过的代码,希望对您有所帮助

  private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {


            if (e.RowIndex >= 0)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    string text = (String)
                     dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    if (text != null)
                        dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
                }
            }  

        }
    }

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(System.String)))
        {
            textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
        }
    }

    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

【讨论】:

  • 感谢您的评论 Ginish 但您的代码也不适用于我:(
  • 你知道为什么你的代码和 MaxOvrdrv 的代码对我不起作用吗?很奇怪
  • 够奇怪的。这段代码在这里完美运行。我正在使用 .net 4.5。你的是什么
  • 是的,我也是。 4.5.1。嗯:S
  • 而不是突出显示整行并拖动,使用上面的代码您是否只需单击任何单元格并拖动到文本框即可检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
相关资源
最近更新 更多