1

我正在尝试编写拖放功能,但遇到了一点困难。

我的问题如下:

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

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

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

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

}

4

1 回答 1

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));
    }
}
于 2013-06-29T08:24:43.757 回答