C# WinForms Drag&;Drop(对象未设置为实例)
我有一个用C#编写的Winforms应用程序 在我的表单上有两个DataGridView 我已经在两个DataGridView之间设置了一个拖放操作,以便从dataContact拖动将int ID发送到dataContactBusiness 但是在删除时,我收到一个错误“对象未设置为实例” 当我逐步浏览我的代码时,我可以看到DragEventArgs e在其数据中包含ID,因此我无法理解为什么会收到错误消息 我的代码如下-C# WinForms Drag&;Drop(对象未设置为实例),c#,winforms,drag-and-drop,C#,Winforms,Drag And Drop,我有一个用C#编写的Winforms应用程序 在我的表单上有两个DataGridView 我已经在两个DataGridView之间设置了一个拖放操作,以便从dataContact拖动将int ID发送到dataContactBusiness 但是在删除时,我收到一个错误“对象未设置为实例” 当我逐步浏览我的代码时,我可以看到DragEventArgs e在其数据中包含ID,因此我无法理解为什么会收到错误消息 我的代码如下- private void dataContact_CellMouseDo
private void dataContact_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow row = dataContact.Rows[e.RowIndex];
int conID = (int)row.Cells["ID"].Value;
dataContact.DoDragDrop(conID, DragDropEffects.All);
}
private void dataContactBusiness_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void dataContactBusiness_DragDrop(object sender, DragEventArgs e)
{
string data = e.Data.GetData(DataFormats.Text).ToString(); //...error occurs here
}
“5”的数据值是我所期望的,那么为什么会出现错误?我猜拖动数据的格式不是DataFormats.Text。
您可以在dataContactBusiness_DragDrop函数中设置断点,并检查DragEventArgs以查看数据的格式,并相应地将参数更改为GetData。是的,就是这样-我发送了一个int,但在e中查找文本。有一次我将DoDragDrop更改为“dataContact.DoDragDrop(conID.ToString(),DragDropEffects.All);”drop事件拾取该值。