1

I have a Winforms app written in C#.

On my form I have two DataGridViews

I have set up a drag and drop operation between the two DataGridViews so that dragging from dataContact sends the int ID across to dataContactBusiness.

However on the Drop I get an error 'object not set to an instance'

When I step through my code I can see that DragEventArgs e contains the ID in its Data, so I fail to see why I am getting the error message.

My code is as follows -

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
}

Error Message

The data value of '5' is what I expected, so why the error?

1 Answer 1

3

I guess the format of the dragged data is not DataFormats.Text . You can put a breakpoint in the dataContactBusiness_DragDrop function and inspect the DragEventArgs to see what the format of the data is and change your parameter to GetData accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that's it - I was sending an int, but looking in e for text. Once I changed the DoDragDrop to 'dataContact.DoDragDrop(conID.ToString(), DragDropEffects.All);' the drop event picks up the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.