tfowler said:Attached is a Class that I created to copy and paste DataGridView contents. Hope it helps.
Todd
Unfortunately, I don't. I pretty much work exclusively in VB (because my company requires that we use it).gprabaka said:I was wondering if you had a C# version of this class.
gprabaka said:I created a VB project and created a class there for just the controlchars and I was able to reference that dll in my C# project.
'**************************************************************************
'convert clipboard ASCII table to an array of DataGridViewCells
'**************************************************************************
Private Shared Function AsciiTableToDataGridViewCellsArray( _
ByVal table As String) As DataGridViewCell(,)
Dim cells(,) As DataGridViewCell
'parse the text into a 2-dimensional array of columns and rows
If table.Contains(Microsoft.VisualBasic.ControlChars.CrLf) _
OrElse table.Contains(Microsoft.VisualBasic.ControlChars.Tab) Then
'split the rows by the CrLf end of line string
Dim separator() As String = {Microsoft.VisualBasic.ControlChars.CrLf}
Dim rows() As String = table.Split(separator, StringSplitOptions.RemoveEmptyEntries)
' ''determine how many columns are in each row
' ''Dim colCount As Integer = 0
' ''Dim foundIndex As Integer = 1
' '' Do
' '' foundIndex = rows(0).IndexOf(Microsoft.VisualBasic.ControlChars.Tab, foundIndex + 1)
' '' If foundIndex > 0 Then
' '' colCount += 1
' '' End If
' '' Loop Until foundIndex <= 0
' '''size the array to fit
' ''ReDim cells(colCount, rows.GetUpperBound(0) + 1)
'get the cells in each row
separator(0) = Microsoft.VisualBasic.ControlChars.Tab.ToString
For r As Integer = 0 To rows.GetUpperBound(0)
Dim cellText() As String = rows(r).Split(separator, StringSplitOptions.None)
''CHANGED: directly redim the cells array based on the actual bounds of the cellText array.
If r = 0 Then ReDim cells(UBound(cellText), rows.GetUpperBound(0) + 1)
'fill the 2-dimensional array with the cell values
For c As Integer = 0 To cellText.GetUpperBound(0)
cells(c, r) = New DataGridViewTextBoxCell
cells(c, r).Value = cellText(c)
Next
Next
Else
'there is only a single cell
ReDim cells(0, 0)
cells(0, 0) = New DataGridViewTextBoxCell()
cells(0, 0).Value = table
End If
Return cells
End Function
Public Sub CopySelection()
Clipboard.Clear()
Clipboard.SetText(Me.SelectedCells.ToString(), TextDataFormat.Text)
End Sub
Public Sub Paste(ByVal overwriteSelectiona As Boolean)
Dim str As String = Clipboard.GetText()
Dim Rows() As String = str.Split(vbNewLine)
Dim RowCount As Integer = 0
Dim ColCount As Integer = 0
Dim TrimChars() As String = New String() {vbNewLine, vbTab, " "}
For Each row As String In Rows
Dim RowArray() As String = row.Split(vbTab)
RowArray(0) = RowArray(0).Trim()
ColCount = 0
For Each cellValue As String In RowArray
If (ColCount < (Me.ColumnCount - Me.SelectedCells(0).ColumnIndex) And RowCount < (Me.RowCount - Me.SelectedCells(0).RowIndex)) Then
Me.Rows(RowCount + Me.SelectedCells(0).RowIndex).Cells(ColCount + Me.SelectedCells(0).ColumnIndex).Value = cellValue.Trim(" " & vbNewLine & vbTab)
End If
ColCount += 1
Next
RowCount += 1
Next
End Sub