How to pase data from excel to datagridview using clipboard?

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

Hi,

Could someone help me,
I'am looking for samplecode how to paste data in a datagridview



Thanks in advance
Marc.
 
Here is something to get you started:

'----------------------------------------
Private Sub btnGetDataFromClipboard_Click(...) Handles
btnGetDataFromClipboard.Click
Dim ClipBdata As IDataObject = Clipboard.GetDataObject
Dim str1, str2(), str3() As String
Dim dt As DataTable = New DataTable
Dim dr As DataRow

str1 = CStr(ClipBdata.GetData(DataFormats.Text))
str2 = str1.Split(CChar(vbCrLf))
str3 = str2(0).Split(CChar(vbTab))
For i As Integer = 0 To str3.GetUpperBound(0)
dt.Columns.Add("col" & i.ToString, Type.GetType("System.String"))
Next
For i As Integer = 0 To str2.GetUpperBound(0)
str3 = str2(i).Split(CChar(vbTab))
dr = dt.NewRow
For j As Integer = 0 To str3.GetUpperBound(0)
For Each col As DataColumn In dt.Columns
dr(col.ColumnName) = str3(j)
Next
Next
dt.Rows.Add(dr)
Next
dgrv1.DataSource = dt
End Sub
'----------------------------------------


On the form I have a button called

btnGetDataFromClipboard

and I also have a datagridview control that I call

dgrv1

I copy the clipboard contents to a string var called str1. Then I parse
out the rows and then parse out the columns and write the contents to a
data table called dt. Then I set the datagridview source data to the
data table.

HTH

Rich
 
Hi Rich,

Many thanks for your help,
its a good start ...

Best wishes for 2009...........
Marc.
 
Hi Rich,

Many thanks for your help,
its a good start ...

Best wishes for 2009...........
Marc.
 
Back
Top