Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

Exploring the .NET PropertyGrid in depth

Rate me:
Please Sign up or sign in to vote.
2.11/5 (5 votes)
26 Jun 2007CPOL2 min read 32K   431   13  
Expolring the flexibilities that PropertyGrid offers to developers.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {

            Customer c1 = new Customer();
            c1.Name = "Bala";
            c1.Gender = genderValues.Male; 
            propertyGrid1.SelectedObject = c1;
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            GridItem itm = e.ChangedItem;
           // ;

            if (itm.Label == "Gender")//Checking for the property Gender 
            {
                if (itm.Value.ToString()=="Male"  ) // Checking for the valu eof the Gender property
                {
                    string[] s1 ={ "M-10000", "M-20000" };
                    HE_GlobalVars._LovList = s1;               
                }
                else
                {
                    string[] s2 ={ "F-10000", "F-20000" };
                    HE_GlobalVars._LovList = s2; 
                }
            }
            
        }
    }

    [DefaultProperty("Name")]
    public class Customer
    {

        private string _Name;
        private genderValues _gender;
        private string _lov;

        [Category("PERSONAL_CAT")]
        public string Name
        {
            get
            {
                return (_Name);
            }
            set
            {
                _Name=value;
            }
        }


        [Category("PERSONAL_CAT")]
        public genderValues  Gender
        {
            get
            {
                return (_gender);
            }
            set
            {
                _gender = value;
            }
        }

        [TypeConverter(typeof(LovList)), CategoryAttribute ("PERSONAL_CAT")]
        public string LOV
        {
            get
            {
                return (_lov );
            }
            set
            {
                _lov = value;
            }
        }


    }

    public enum genderValues
    {
        Male,
        Female
    }

    //Class for setting the Drop down values
    internal class HE_GlobalVars
    {
        internal static string[] _LovList;
    }


    public class LovList : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            //Default values for the drop down is set here
            string[] s1 ={ "M-10000", "M-20000" };
            HE_GlobalVars._LovList = s1;   
            //true means show a combobox
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            //true will limit to list. false will show the list, 
            //but allow free-form entry
            return true;
        }

        public override System.ComponentModel.TypeConverter.StandardValuesCollection
               GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(HE_GlobalVars._LovList);
        }
    }




}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions