Copy excel data to datagrid

Helpmeh

Member
Joined
Nov 10, 2014
Messages
13
Programming Experience
1-3
Hi,

i have a windows form with a datagrid, and i want the user to be able to copy and paste the data i am going to perform calculations on, into the grid in a certain column. For simpilicity, we can assume that they have copied the correct numerical values to the clipboard from excel, and they have selected the correct cell in the correct column in the datagrid to paste it into. I would also like one value per cell as found in excel.

so in lehmann's terms i would like the procedure to be:

if the clipboard contains data
for each piece of data/value in the clipboard
copy value into the cell
go to the next row below and copy next piece of data etc.

Any help would be greatly appreciated in pointing me in the right direction :)

Thanks
 

rudalboy09

Member
Joined
Nov 28, 2014
Messages
8
Programming Experience
Beginner
can you explain with a picture? we don't know what you want to do, how we can help you sir?
 

Helpmeh

Member
Joined
Nov 10, 2014
Messages
13
Programming Experience
1-3
can you explain with a picture? we don't know what you want to do, how we can help you sir?
For those interested in this i managed to come up with code that does exactly stated, as described above. And for people wanting to use the microsoft copy and paste feature for other purposes, the copy function saves the values (excel data in my case) as a string onto the clipboard. In order to access that information using vb then you need to use the split function.

Hope the code below helps some people:

Public Sub PasteData(ByRef DataGridView1 As DataGridView)


Dim ClipValArr() As String
Dim i, ii, iii As Integer
Dim ColIndx, ColCounter, RowIndx As Integer

ClipValArrLength = Clipboard.GetText().Split(Environment.NewLine)
RowIndx = DataGridView1.SelectedCells(0).RowIndex
ColIndx = DataGridView1.SelectedCells(0).ColumnIndex
For i = 0 To ClipValArrLength.Length - 1
iii = iii + 1
If ClipValArrLength(i) <> "" Then
ClipValArr = ClipValArrLength(i).Split(vbTab)
ColCounter = ColIndx
For ii = 0 To ClipValArr.Length - 1
If ColCounter > DataGridView1.ColumnCount - 1 Then Exit For
If RowIndx > DataGridView1.Rows.Count - 1 Then Exit Sub
With DataGridView1.Item(ColCounter, RowIndx)
.Value = ClipValArr(ii).TrimStart
End With


ColCounter = ColCounter + 1
Next
RowIndx = RowIndx + 1
End If
If iii >= 14 Then
dataset1.Tables(0).Rows.Add(dataset1.Tables(0).NewRow())


End If
Next


End Sub

Thanks
 
Top Bottom