11-11-15, 11:51 AM
ربما تبحث عن كود لعمل نسخ من DataGridView الى اكسل او العكس
الكود هو
Copy
Paste
الكود هو
Copy
كود :
Sub Copy(ByVal DGV As DataGridView)
If DGV.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
Try
DGV.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText
Clipboard.SetDataObject(DGV.GetClipboardContent())
Catch ex As System.Runtime.InteropServices.ExternalException
MsgBox(ex.Message)
End Try
End If
End Sub
Paste
كود :
Private Sub Paste(ByVal DGV As DataGridView)
DGV.Rows.Clear()
DGV.Columns.Clear()
Dim s As String
Dim i, ii As Integer
Dim arT() As String
Try
s = Clipboard.GetText()
Dim tArr() As String = s.Split(ControlChars.NewLine)
arT = tArr(0).Split(vbTab)
Dim word As String
For Each word In arT
DGV.Columns.Add(word, word)
Next
Dim cc, iRow, iCol As Integer
For i = 1 To tArr.Length - 2
DGV.Rows.Add("")
If tArr(i) <> "" Then
arT = tArr(i).Split(vbTab)
cc = iCol
For ii = 0 To arT.Length - 1
If cc > DGV.ColumnCount - 1 Then Exit For
If iRow > DGV.Rows.Count - 1 Then Exit Sub
With DGV.Item(cc, iRow)
.Value = arT(ii).TrimStart
End With
cc = cc + 1
Next
iRow = iRow + 1
End If
Next
Catch ex As Exception
MsgBox("Please redo Copy and Click on cell")
End Try
End Sub