Copying contents of PropertyGrid

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
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.
 
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.
 
marble_eater said:
That isn't really something you can do directly.

Oh yes you can:

Code:
  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
 
Last edited:
Back
Top