Hi, Could anyone tell me the best way to implement a propertygrid?

currently I'm doing it like this but I'm having truble setting the value of the private variable with the values returned but a single SqlDataReader:

[DefaultPropertyAttribute("ObjectTypeID")]
    public class ObjectType
    {

        private int _objTypeID;
        private int _stateType;
        private int _state;

        [CategoryAttribute("Object State Defaults"), DescriptionAttribute("Object type ID")]
        public int ObjectTypeID
        {
            get
            {
                return _objTypeID;
            }
            set
            {
                _objTypeID = value;
            }
        }

        [CategoryAttribute("Object State Defaults"), DescriptionAttribute(" state type of Object type")]
        public int StateType
        {
            get
            {
                return _stateType;
            }
            set
            {
                _stateType = value;
            }
        }

        [CategoryAttribute("Object State Defaults"), DescriptionAttribute("state of object type")]
        public int State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
            }
        }
}

Thanks in advanced

Chris

but I'm having truble setting the value of the private variable with the values returned but a single SqlDataReader:

Can you be so kind to clarify?

Can you be so kind to clarify?

I've managed to set the value of the private variables, it was just due to my inexperience with C#.

I idealy wanted the attributes to be automatucally changed depending on the selected object as attributes differ from object to object.

For the sake of my project I have created the property grid as shown above but if you know how the attributes can be automatically set depending on the selected object that would be great.

Thanks for your help it's much appreciated.

If the new object to set, is structurally distinct of the previous one, set the property grid to a new one(oldpropertygrid = new PropertyGrid;), then set the selected object.

Hope this helps

Here is an example, that might help:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Feb17Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Create the customer object we want to display
            Customer bill = new Customer();
            // Assign values to the properties
            bill.Age = 50;
            bill.Address = "114 Maple Drive ";
            bill.DateOfBirth = Convert.ToDateTime(" 5.11.1979");
            bill.SSN = "123-345-3566";
            bill.Email = "bill@aol.com";
            bill.Name = "Bill Smith";
            // Sets the the grid with the customer instance to be
            // browsed
            PropertyGrid propertyGrid1 = new PropertyGrid();
            propertyGrid1.Location = new Point(10, 10);
            propertyGrid1.Size = new Size(250, 400);
            this.Controls.Add(propertyGrid1);
            propertyGrid1.SelectedObject = bill;

        }
    }

    [DefaultPropertyAttribute("Name")]
    public class Customer
    {
        private string _name;
        private int _age;
        private DateTime _dateOfBirth;
        private string _SSN;
        private string _address;
        private string _email;
        private bool _frequentBuyer;

        // Name property with category attribute and 
        // description attribute added 
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer")]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        //[Browsable(false)]  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! <<<< if you use it, it will be excluded from the dataSource!
        [CategoryAttribute("ID Settings"),
        DescriptionAttribute("Social Security Number of the customer")]
        public string SSN
        {
            get
            {
                return _SSN;
            }
            set
            {
                _SSN = value;
            }
        }
        [CategoryAttribute("ID Settings"),
        DescriptionAttribute("Address of the customer")]
        public string Address
        {
            get
            {
                return _address;
            }
            set
            {
                _address = value;
            }
        }        
        [CategoryAttribute("ID Settings"),
        DescriptionAttribute("Date of Birth of the Customer (optional)")]
        public DateTime DateOfBirth
        {
            get
            {
                return _dateOfBirth;
            }
            set
            {
                _dateOfBirth = value;
            }
        }
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")]
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }
        [CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer as bought more than 10 times, this is set to true")]
        public bool FrequentBuyer
        {
            get
            {
                return _frequentBuyer;
            }
            set
            {
                _frequentBuyer = value;
            }
        }
        [Browsable(false)]
        [CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
            }
        }
        public Customer()
        {
        }
    } 
}

I don't clearly understand what you mean in ' idealy wanted the attributes to be automatucally changed depending on the selected object as attributes differ from object to object'

As far I can see what is changing is the value of each attribute, not the structure.
So I'll try to explain what to do with the values.

Once you asigned the bill to the selected object, you can catch the changes on the SelectedObjectChanged event of the property grid. To do so you must add the event handler for it.

You may also include a 'save' button in the form to retrieve the current selected object of the property grid, convert it to your Customer class and save the values some where.

Then, if needed, you can create a new customer and set the new values before assignint it to the selected object.

Assigning a new Customer without setting the values, will create an empty property grid to be filled by the user.

Hope this helps

I managed set the attributes of the property grid manually using an example off the internet a while ago and its working fine.

What i was wondering was how do i create the property grid attributes using the column names from a batabase table rather than setting them myself (columns may change so manually creating attributes means they cannot be changed without going into the code)

for example i might have two object obj1 with Name; Area and diameter attributes and obj2 with Name; Ares and Number of Sides attributes. The two objects have different attributes so manually setting them would result in redundent attributes.

Sorry i am fairly rubbish at explaining this.

Then I'll go back to my first answer.

Lets the Form1 to be:

PropertyGrid propertyGrid1;
      public Form1()
        {
            InitializeComponent();

            propertyGrid1 = new PropertyGrid();
            propertyGrid1.Location = new Point(10, 10);
            propertyGrid1.Size = new Size(250, 400);
            this.Controls.Add(propertyGrid1);

        }

Then, where you will need to set the SelectedObject, write some code like like:

If (propertyGrid1.SelectedObject==null){
      propertyGrid1.SelectedObject = ObjectOfClassA;}
else{
      propertyGrid1 = new PropertyGrid();
      propertyGrid1.Location = new Point(10, 10);
      propertyGrid1.Size = new Size(250, 400);
      propertyGrid1.SelectedObject = ObjectOfClassA_orB_orWhatElse;}

Hope this helps

commented: Has been extremely helpfull and happy to help. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.