how do I retrieve data from a PropertyGrid?

Posted on 2010-01-07
Last Modified: 2013-11-26
I am trying to retrieve the data out of a PropertyGrid. the grid is filled by a complex series of methods and classes, and changes on each different selection that the user makes in another screen. since the PropertyGrid can have different values and has different methods to fill these values it would be easiest to take what is there. I have found something that I could use but it is in vb and i cannot seem to get it to work in c#. the article was in the database called how to iterate through a PropertyGrid.
here is the vb code:

Imports System.ComponentModel

        Dim pg As New PropertyGrid
        Dim pd As PropertyDescriptor
        Dim pdc As PropertyDescriptorCollection

        pg.SelectedObject = TextBox1

        pdc = TypeDescriptor.GetProperties(pg.SelectedObject, True)

        For Each pd In pdc

            Debug.Write(pd.Name & " = ")


thanks in advance for the help
Question by:wmm2464
    LVL 3

    Expert Comment

    You can grab the properties of the item that the grid is pointing to.
    LVL 9

    Accepted Solution

    Looks like you only want to translate the VB code to C#
            private void button1_Click(object sender, EventArgs e)
                propertyGrid1.SelectedObject = textBox2;
                PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject, true);
                foreach (PropertyDescriptor pd in pdc)
                    Console.WriteLine(pd.Name + "=" + pd.GetValue(propertyGrid1.SelectedObject));

    Author Comment

    ok thanks for the translation, it does work after a fasion, the issue i am having is the sub arrays under the main do not get shown when i do this, i am filling a text box with the information that is created by the getjournal method this method has a main class that is filled and displayed but also abstract classes that are filled. for instance i have a log record field that has multiple arrays and items below it. is there a way that i can retrieve the data that are in these abstract classes? the property grid has no problem using the data and getting filled. i would think that once it is filled that it would be just as simple as iterating through it to retrieve all rows.

            protected virtual void OnSelectedTransactionChanged()
                if (selectedTransaction != null)
                    journalPropertyGrid.SelectedObject = selectedTransaction.GetJournal();
                    StringBuilder str = new StringBuilder();

                    PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(journalPropertyGrid.SelectedObject, true);

                    foreach (PropertyDescriptor pd in pdc)
                        str.AppendFormat("{0} =   ", pd.Name);
                        str.AppendFormat("{0}", (pd.GetValue(journalPropertyGrid.SelectedObject)));
                    txtJournal.Text = str.ToString();

                    // Highlight the Transaction in the MemoryDump

                    offsetToolStripStatusLabel.Text =
                        string.Format(Resources.StatusOffset, selectedTransaction.Offset);

                    sizeToolStripStatusLabel.Text =
                        string.Format(Resources.StatusSize, selectedTransaction.Size);
                    journalPropertyGrid.SelectedObject = null;
                    memoryDumpControl.Highlight(0, 0);


    the result set is this:
    BucketNumber =   86
    SlotNumber =   1607
    Address =   177220
    Product =   Lotto
    Length =   336
    LogRecordLength =   276
    InputMessageLength =   16
    OutputMessageLength =   26
    LogRecord =   WaLotto.Mjf.MatrixLogRecord
    InputMessage =   System.Byte[]
    OutputMessage =   System.Byte[]
    Size =   336
    Offset =   6452

    what i really need is all of the data that is within the log record, there are quit a few rows of data there.

    thanks for the help so far
    LVL 9

    Expert Comment


    Author Comment

    yes i have already reviewed this code, thank you. it is simular but it basically fills the property grid and i need to retrieve data from the grid without access to the classes that are used to fill it.

    Author Comment

    Thank you for the assistance! you all have been very helpfull

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now