Copy and paste to Excel (or other)

8 posts, 0 answers
  1. Llorens Mathieu
    Llorens Mathieu avatar
    19 posts
    Member since:
    Jan 2009

    Posted 30 Oct 2009 Link to this post

    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.
  2. Tiago
    Tiago avatar
    25 posts
    Member since:
    Aug 2009

    Posted 30 Oct 2009 Link to this post

    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!
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Llorens Mathieu
    Llorens Mathieu avatar
    19 posts
    Member since:
    Jan 2009

    Posted 02 Nov 2009 Link to this post

    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).
  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 03 Nov 2009 Link to this post

    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.
  6. matt
    matt avatar
    4 posts
    Member since:
    Aug 2011
  7. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 19 Aug 2011 Link to this post

    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 >>

  8. ceetee
    ceetee avatar
    4 posts
    Member since:
    Sep 2012

    Posted 02 Sep 2011 Link to this post

    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
  9. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 07 Sep 2011 Link to this post

    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 >>

Back to Top
UI for WinForms is Visual Studio 2017 Ready