Datagrid Gridview拖放事件

时间:2013-06-29 08:00:18

标签: c#

我正在尝试编写一个拖放功能,而我只是有点困难。

我的问题如下:

1。)在附加代码的调整中,首先应调用Load_Load事件,或者它的序列是否重要(如您所见,它是在这组代码中调用的最后一个事件)。

2。)拖放事件有效,但是当我单击一个单元格或列标题时,在网格二上我得到:mscorlib.dll中出现未处理的“System.ArgumentOutOfRangeException”类型异常。我该如何解决这个问题?

3.)在数据网格2中,单元格0和1编号为单元格0和单元格1.当我将鼠标悬停在两个单元格上时,我得到0-1表示单元格0和1-2表示单元格1 - 其余的单元格标题是没有编号或有工具提示。这是如何解决的?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Suite_Estimation
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(System.String)))
         {
            Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));               
            dataGridView1.Rows[dataGridView1.HitTest(clientPoint.X,      clientPoint.Y).RowIndex].Cells[dataGridView1.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex].Value = (System.String)e.Data.GetData(typeof(System.String));

         }
}

    private void dataGridView1_DragEnter(object sender, DragEventArgs e)
    {
     if (e.Data.GetDataPresent(typeof(System.String)))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }

    private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
     dataGridView2.DoDragDrop(dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), DragDropEffects.Copy);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewRow dr = new DataGridViewRow();

        dataGridView2.Rows.Add(5);
        dataGridView2.Rows[0].Cells[0].Value = "00000000";
        dataGridView2.Rows[1].Cells[0].Value = "11111111";
        dataGridView2.Rows[2].Cells[0].Value = "22222222";
        dataGridView2.Rows[3].Cells[0].Value = "33333333";
        dataGridView2.Rows[4].Cells[0].Value = "44444444";
        dataGridView2.Rows[0].HeaderCell.Value = "0 - 1";
        dataGridView2.Rows[1].HeaderCell.Value = "1 - 2";
    }
}

}

1 个答案:

答案 0 :(得分:0)

试试这个

 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
                dataGridView1.DoDragDrop(
                   dataGridView1.Rows[e.RowIndex]
                                .Cells[e.ColumnIndex]
                                .Value.ToString(), 
                   DragDropEffects.Copy);
        }

private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(System.String)))
    {
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
        int rowIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
        int colIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex;
        if (rowIndex > -1 && colIndex > -1)
            dataGridView1.Rows[rowIndex].Cells[colIndex].Value =  
                           (System.String)e.Data.GetData(typeof(System.String));
    }
}