This is a migrated thread and some comments may be shown as answers.

Copy and paste to Excel (or other)

7 Answers 654 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Llorens Mathieu
Top achievements
Rank 1
Llorens Mathieu asked on 30 Oct 2009, 08:32 AM
Hi,
Is it possible to copy values from the GridView using a kind of CTRL-A + CTRL-C and paste it to Excel using CTRL-V ?
I'd like to implement this functionnality in my application. It will be a quick alternative to an Excel export.

7 Answers, 1 is accepted

Sort by
0
Tiago
Top achievements
Rank 2
answered on 30 Oct 2009, 05:22 PM
I never heard of it ... but i don't believe that would be faster than an export to excel because you can create a button to export the Grid and when the export is done you can ask the user if he want's to open the exported file ...

Hope it helps!
0
Llorens Mathieu
Top achievements
Rank 1
answered on 02 Nov 2009, 07:46 AM
I have done some WPF apps before with the grid component of microsoft and copy/paste is possible without coding any line of code. You just have to make selection with your mouse, ctrl-c and ctrl-v in an Excel worksheet and it's done.

It's perhaps not very much faster than an "export" button but it's very intuitive for a Microsoft Windows user (Copy/Paste is everywhere in Windows apps).
0
Martin Vasilev
Telerik team
answered on 03 Nov 2009, 07:44 AM
Hi Llorens Mathieu,

Thank you for the question.

Although RadGridView does not support Copy/Paste to Excel out-of-the-box, you can implement it on your own. Please, take a look at the following KB Article. Write me back if you have any additional questions.

Sincerely yours,
Martin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
matt
Top achievements
Rank 1
answered on 16 Aug 2011, 02:31 PM
0
Martin Vasilev
Telerik team
answered on 19 Aug 2011, 08:16 AM
Hello Matt,

Thank you for contacting us.

To convert code from C# to VB and vice verse you can use this code converter provided by Telerik. For your convenience I am posting the result here:
Private Sub radGridView1_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.C AndAlso e.Control Then
        'you could change the code below according to your custom logic   
        Dim copyStr As String = ConvertSelectedDataToString(Me.radGridView1)
        Clipboard.SetDataObject(copyStr)
    End If
End Sub
 
''' <summary>  
''' Prepare string in format suitable to paste in Excel sheet  
''' </summary>  
''' <param name="grid">Source selection RadGridView </param>  
''' <returns>Formated string for clipboard</returns>  
Private Function ConvertSelectedDataToString(grid As RadGridView) As String
    Dim strBuild As New StringBuilder()
 
    Dim row As Integer = 0
    While row < grid.SelectedRows.Count
        Dim cell As Integer = 0
        While cell < grid.SelectedRows(row).Cells.Count
            strBuild.Append(grid.SelectedRows(row).Cells(cell).Value.ToString())
            strBuild.Append(vbTab)
            System.Math.Max(System.Threading.Interlocked.Increment(cell),cell - 1)
        End While
 
        strBuild.Append(vbLf)
        System.Math.Max(System.Threading.Interlocked.Increment(row),row - 1)
    End While
 
    Return strBuild.ToString()
End Function

Hope this helps. Let me know if you have any other questions.

Kind regards,
Martin Vasilev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
ceetee
Top achievements
Rank 1
answered on 02 Sep 2011, 09:12 AM
I have done this programatically on a grid with hierarchy

The code is specific, it doesn't copy parent/child data by design (as this is what was required) however you should be able to bend it to your needs

Just thought i'd share it incase anyone found it useful
 
Private Sub rgvResults_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rgvResults.KeyDown
      Dim radGridView As RadGridView = TryCast(sender, RadGridView)
      Console.WriteLine("KeyDown" + radGridView.Name)
      If e.KeyCode = Keys.C AndAlso e.Control Then
          GridClipBoardCopySelected(radGridView)
      End If
  End Sub
 
  Private Sub GridClipBoardCopySelected(ByVal grid As RadGridView)
      Dim bCellsOnrow As Boolean
      Dim sbOutput As New StringBuilder
 
      For Each row As GridViewRowInfo In grid.Rows
          bCellsOnrow = False
          'are there selected cells on the parent?
 
          For Each cell As GridViewCellInfo In row.Cells
              If cell.IsSelected Then
                  sbOutput.Append(Trim(cell.Value)).Append(vbTab)
                  bCellsOnrow = True
              End If
          Next
 
          If bCellsOnrow Then sbOutput.Append(vbCrLf)
 
          If row.HasChildRows And Not bCellsOnrow Then
              bCellsOnrow = False
 
              Dim childcolumncount = grid.Templates(0).ColumnCount
              Dim bMatchOnRow As Boolean
              Dim iFoundFieldCount As Integer
 
              'loop through the child rows
              For i = 0 To row.ChildRows.Count - 1
 
                  iFoundFieldCount = 0
                  For c = 0 To childcolumncount - 1
                      If row.ChildRows(i).Cells(c).IsSelected Then
 
                          'add a tab if this ISNT the first cell.
                          If iFoundFieldCount > 0 Then sbOutput.Append(vbTab)
 
                          sbOutput.Append(Trim(row.ChildRows(i).Cells(c).Value))
                          iFoundFieldCount = iFoundFieldCount + 1
                          bCellsOnrow = True
                      End If
                  Next
                  If bCellsOnrow Then sbOutput.Append(vbCrLf)
 
              Next
          End If
      Next
 
      If sbOutput.Length > 0 Then
          Clipboard.Clear()
          Clipboard.SetText(Trim(sbOutput.ToString))
      End If
 
  End Sub
0
Jack
Telerik team
answered on 07 Sep 2011, 02:37 PM
Hello Chris,

Yes, this is a specific scenario, however thank you for sharing your code with the community. I am sure it will help someone. In case you have any further questions, do not hesitate to ask.

All the best,
Jack
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Llorens Mathieu
Top achievements
Rank 1
Answers by
Tiago
Top achievements
Rank 2
Llorens Mathieu
Top achievements
Rank 1
Martin Vasilev
Telerik team
matt
Top achievements
Rank 1
ceetee
Top achievements
Rank 1
Jack
Telerik team
Share this question
or