Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
How could I copy the contents of a propertygrid to the clipboard so that the user can paste them to Excel or wherever? It seems a simple enough thing to want to do, but I can't figure it out at all.
  • Leaders
Posted
That isn't really something you can do directly. You could use reflection to enumerate over the properties of an object, copying the properties that don't have a Browsable(false) attribute attatched to them, but how you get that into Excel, I wouldn't know.
[sIGPIC]e[/sIGPIC]
  • 2 weeks later...
Posted (edited)
That isn't really something you can do directly.

 

Oh yes you can:

 

  Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
   
       Dim v As String

       With Me.prgOne

           If Not .SelectedGridItem Is Nothing Then

               'There's a parent GridItem at the top of the hierarchy, which is not shown. Find that.
               Dim f As GridItem = .SelectedGridItem
               Do Until f.GridItemType = GridItemType.Root
                   f = f.Parent
               Loop

               For Each h As GridItem In f.GridItems

                   GridItemToString(v, h)

                   If h.GridItemType = GridItemType.Category Then
                       For Each h2 As GridItem In h.GridItems
                           GridItemToString(v, h2)
                       Next
                       v = v & vbCrLf
                   End If

               Next

               Clipboard.SetText(v)

           Else
               'only ever seems to be the case if the SelectedObject is Nothing, so not a problem.
           End If
       End With

   End Sub

   Private Sub GridItemToString(ByRef v As String, ByVal gi As GridItem)

       With gi
           v = v & .Label & vbTab
           If Not .Value Is Nothing Then v = v & .Value.ToString
           v = v & vbCrLf
       End With

   End Sub

Edited by rbulph

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...