DataGrid copy/paste from Excel

  • Thread starter Thread starter Faith
  • Start date Start date
F

Faith

Hello.
I need to take a column from Excel(unknown amount of rows)
that will be selected by the user and copy those cells.
Then I will need to paste those cells into the first
column in a Data Grid (system.windows.forms).
Basically, the user receives a list of clientIDs from a
client as an email attachment in Excel listed like:
1234 ABC Company
234 DEF Company
348 GHI Company
....and so on
The user copies the first column of clientIDs and will
need to paste them into a data grid in a program. Is
there a way to do this??

Thanks so much for your help!
Faith
 
Hi,

You should bind the grid to a data source (DataTable, for example). Then,
react on Ctrl-V shortcut (as well as Edit->Paste etc. etc.) and upon the
user attempting to paste the data,
examine what's available on the clipboard, parse the pasted data if
necessary and populate the corresponding column of the bound data table with
the parsed client ID values.
 
Faith,

This is not the most stable sample, you will want to do a
lot of checking with the data before you place it in the
grid, but you can see where it is going

Dim t As IDataObject
Dim row(0) As String

t = Clipboard.GetDataObject()
Dim sr As New System.IO.StreamReader(CType
(t.GetData("csv"), System.IO.MemoryStream))

Do Until sr.Peek = -1
row(0) = sr.ReadLine
ds.Tables(0).Rows.Add(row)
Loop

kirk
 
Hi Faith,

If you want to copy a column in an Excel worksheet to the datagrid, you can
try using OleDB to achieve this. Here's a KB article for you to see how to
connect to an Excel file using OleDB.

http://support.microsoft.com/default.aspx?scid=kb;en-us;311731

Based on the KB, you have to define a name which indicates the column you
want to copy in the Excel file. This can be done by VBA. If you don't want
the blank cells to be copied, just add WHERE columnname <> NULL after the
SELECT clause.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Content-Class: urn:content-classes:message
| From: "Faith" <[email protected]>
| Sender: "Faith" <[email protected]>
| Subject: DataGrid copy/paste from Excel
| Date: Mon, 6 Oct 2003 12:01:46 -0700
| Lines: 17
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOMPEnLwuvaLw2ZRyivWZ5gJYcj1w==
| Newsgroups: microsoft.public.dotnet.general
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:110924
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| Hello.
| I need to take a column from Excel(unknown amount of rows)
| that will be selected by the user and copy those cells.
| Then I will need to paste those cells into the first
| column in a Data Grid (system.windows.forms).
| Basically, the user receives a list of clientIDs from a
| client as an email attachment in Excel listed like:
| 1234 ABC Company
| 234 DEF Company
| 348 GHI Company
| ...and so on
| The user copies the first column of clientIDs and will
| need to paste them into a data grid in a program. Is
| there a way to do this??
|
| Thanks so much for your help!
| Faith
|
 
Back
Top