Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I got the requirement where I need to drag and drop the data from Excel to a DataGridView in a Windows Application.

My requirement is I have a grid with some Columns and now I want to drag the complete column data in to the specified Column.

Can any one help me how to do that one.

Thanks in advance.

Aditya.
Posted
Updated 5-Apr-11 8:54am
v2
Comments
wizardzz 5-Apr-11 14:54pm    
Tried to correct random capitalization

1 solution

This is not a complete answer to your question but it might give you a starting point and something to work on while you are waiting for someone to give a complete answer.

Make sure to set the AllowDrop property of your dgv to true.

Then paste in the code from the example on this page[^]

In the Events tab of the Properties Window double click on the DragDrop event to get an empty event handler and call GetAllFormats() from this event handler.

You might want to change the TextBox in the example to a ListBox and add each format to that.

Then try dropping some data from Excel onto your dgv.

I believe that when you copy data from Excel to the Clipboard it copies it as HTML and as a String (or at least it used to), it may also use some other formats as well and I assume that it does the same sort of thing for Drag-Drop. Once you know the DataFormats available to you, you can experiment with them in turn to decide how to handle each and discover how the data is organized. For example for the String DataFormat you could load the data into a TextBox to examine it.

In case you aren't familiar with the process this[^] page has an example of handling dropped data (it's for a ListBox but the principle is the same). Look at the ListDragTarget_DragDrop method and modify it to display the data in an appropriate control.

Hope this helps and that you get a more complete answer. :)
 
Share this answer
 
v2
Comments
aditya kiran maroju 6-Apr-11 2:23am    
Hi Henry thank you for your reply. i have done the basic drag and drop functionality the thing that i require is how to place the specific columms in to the specific columns of the grid. For Example in my excel i am having column (Employee Name) data i have selected all the Employee Name data from the excel and Dragged it into the Grid Now i want the data to be inserted into the Employee Name Columns of the Grid. I have two or more columns in my grid.

Thanks Aditya.
Henry Minute 6-Apr-11 6:26am    
How to do that in the easiest way really depends on how the incoming data is formatted. If it is something like a delimited list (csv or similar) then it can be made into an array by String.Split() otherwise you will have to convert it into some form of collection (List<string> for example).

Then it should be a simple matter of iterating over the Rows collection and inserting the data in the appropriate column.
for (int i = 0; i < myDataGridView.Rows.Count; i++)
{
DataGridViewRow row = myDataGridView.Rows[i];
row["thecolumnyouareinterestedin"].Value = incomingDataList[i];
}
or similar.
aditya kiran maroju 7-Apr-11 3:47am    
Hi Henry Thank you for your help i have solved the problem.

Cheers.
Henry Minute 7-Apr-11 4:57am    
Good stuff!

Well done. :)
MiteshTemare 10-Jun-14 3:51am    
henry please give us full demop for excel drag and drop on to grid view..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900