将值从Excel拖放到Windows应用程序中的DataGridView [英] Drag and Drop values from Excel to DataGridView in a Windows Application

查看:139
本文介绍了将值从Excel拖放到Windows应用程序中的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到了需要将数据从Excel拖放到Windows应用程序中的DataGridView的要求.

我的要求是我有一个包含一些列的网格,现在我想将完整的列数据拖到指定的列中.

谁能帮我做那个.

提前谢谢.

Aditya.

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.

推荐答案

这不是您问题的完整答案,但它可能会为您提供一个起点,并且在您等待某人提供帮助时可以做一些事情.完整答案.

确保将dgv的AllowDrop属性设置为true.

然后将示例中的代码粘贴到此页面 [ ^ ]

在属性"窗口的事件"选项卡中,双击DragDrop事件以获取一个空的事件处理程序,然后从该事件处理程序中调用GetAllFormats().

您可能需要将示例中的TextBox更改为ListBox,然后将每种格式添加到该格式.

然后尝试将一些数据从Excel拖放到dgv中.

我相信,当您将数据从Excel复制到剪贴板时,它会将其复制为HTML和字符串(或者至少是以前使用的字符串),它也可能会使用其他格式,并且我认为它也可以做同样的事情用于拖放.知道可用的数据格式后,您可以依次尝试使用它们来决定如何处理每种格式,并发现数据的组织方式.例如,对于String DataFormat,您可以将数据加载到TextBox中以进行检查.

如果您不熟悉该过程,请 [^ ]页面上有一个处理丢弃数据的示例(它用于ListBox,但原理相同).查看ListDragTarget_DragDrop方法并对其进行修改以在适当的控件中显示数据.

希望这对您有所帮助,并希望您获得更完整的答案. :)
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. :)


这篇关于将值从Excel拖放到Windows应用程序中的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆