简体   繁体   English

无法将文本从DataGridView拖放到TextBox C#

[英]Unable to drag and drop text from DataGridView to TextBox C#

I have been using code that others online have supplied but for some reason it won't let me drag items from the datagridview to the textbox. 我一直在使用其他人在线提供的代码,但出于某种原因,它不会让我将项目从datagridview拖到文本框中。 I highlight a row in the dataGridView and try to drag it to the textbox but nothing happens. 我在dataGridView中突出显示一行并尝试将其拖动到文本框但没有任何反应。 I have also enabled the drop property for the textBox but still no difference. 我也为textBox启用了drop属性,但仍然没有区别。 Here's the code that I am using: 这是我正在使用的代码:

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

Here is a small sample that i have done to give you an idea on how to do this... works perfectly for me. 这是一个小样本,我已经做过让你知道如何做到这一点......对我来说非常合适。 I used WinForms here. 我在这里使用了WinForms。 If WPF, there may be some more events you will need to register to in order for the drag+drop to register... 如果是WPF,可能还需要注册一些事件,以便拖放+注册...

Note that you will want to add more code here and there to perform what you really want to do when you drag an item from one control to the other. 请注意,您需要在此处添加更多代码,以便在将项目从一个控件拖动到另一个控件时执行您真正想要执行的操作。

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

can't you use DataGridViewCellMouseEventArgs e instead of hittest for getting row index in dataGridView1_CellMouseDown. 你不能使用DataGridViewCellMouseEventArgs e而不是hittest来获取dataGridView1_CellMouseDown中的行索引。 below is your code modified hope this helps 下面是你的代码修改希望这有帮助

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

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

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