How can I update propertygrid item values when another item changed in winform c#?
Asked Answered
B

1

8

I have a property grid with 2 items. Country & Cities. I have 1 table in database : CountryCityTable that save LocationId, Title , ParentId. For countries parentId = 0 and for cities is countryid.

In my propertygrid, I use these and show in 2 combobox items. Please see my code :

namespace ProGrid
{
    public class KeywordProperties
    {
        [TypeConverter(typeof(CountryLocationConvertor))]
        public string CountryNames { get; set; }

        [TypeConverter(typeof(CityLocationConvertor))]
        public string CityNames { get; set; }
    }
}

And

namespace ProGrid
{
    public class CountryLocationConvertor : StringConverter 
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {            
            HumanRoles Db = new HumanRoles();
            List<LocationsFieldSet> Items = new List<LocationsFieldSet>();
            Items = Db.LoadLocations(0,0);
            string[] LocationItems = new string[Items.Count];
            int count = 0;
            foreach (LocationsFieldSet Item in Items)
            {
                LocationItems[count] = Item.Title;
                count++;
            }
            return new StandardValuesCollection(LocationItems);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;//false : If you want the user to be able to type in a value that is not in the drop-down list.
        }
    }

    public class CityLocationConvertor : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            HumanRoles Db = new HumanRoles();
            List<LocationsFieldSet> Items = new List<LocationsFieldSet>();
            Items = Db.LoadLocations(1,20);
            string[] LocationItems = new string[Items.Count];
            int count = 0;
            foreach (LocationsFieldSet Item in Items)
            {
                LocationItems[count] = Item.Title;
                count++;
            }
            return new StandardValuesCollection(LocationItems);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

And

KeywordProperties Kp = new KeywordProperties();
myPropertyGrid.SelectedObject = Kp;

Now, I want when user changed country title in propertygrid, List of cities updated(just display cities that parentid of these = countryid).

Also, in my class how can i change Number 20 in my code(Db.LoadLocations(1,20);) to selected country id ?

Thank's.

Bullace answered 18/11, 2012 at 12:25 Comment(3)
Apply the [RefreshProperties] attribute.Krueger
Please change my code with this attribute. Thank's.Bullace
Like Hans said, this property works nicely and is very clean. Also check for more detail https://mcmap.net/q/827726/-updating-a-propertygridRonnyronsard
F
3

You will need to implement something similar to INotifyPropertyChanged

Microsoft INotifyPropertyChange Documentation

In other word, you will need to have some event raised when you chance one property. As far as I remember, the property grid automaticly check for that type of event/interface and refresh the correct property node when the event is raised.

The important part is:

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

and

public bool MyBoolProperty
{
    get { return  myBoolField; }
    set
    {
        myBoolField = value;
        NotifyPropertyChanged();
    }
}

If you want to do something that is not covered by the PropertyGrid, you simply need to register your own method in the PropertyChanged event and do whatever you please.

Flatiron answered 18/11, 2012 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.