1
我想写一个拖放功能,我只是有点困难。Datagridgridview拖放事件
我的问题如下:
1)在reguards所附的代码,应该Load_Load事件被称为第一或不要紧,它是按什么顺序(你可以看到,它的最后一个事件称为在这组代码中)。
2.)拖/放事件工作,但是当我点击一个单元格或列标题,在网格二我得到:类型'System.ArgumentOutOfRangeException'的未处理的异常发生在mscorlib.dll中。我该如何解决这个问题?
3.)在数据网格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";
}
}
}
细胞和列标题不使用该代码给异常,但小区现在给System.NullReferenceException时,我对细胞点击拖动。 – user2506869
Actuall工作 - 我将您的代码更改为datagrid2,它的工作原理,但如果我不完全放在单元格中,我会得到一个错误(例如,如果我试图放在没有添加行的标题或空间上给出异常:ArgumentOutOfRangeException。但是,这是非常有用的谢谢你! – user2506869
@ user2506869,请参阅编辑 – yogi