Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 220
Ali Ahmadi Avatar asked Nov 18 '12 12:11

Ali Ahmadi


1 Answers

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.

like image 183
LightStriker Avatar answered Oct 12 '22 18:10

LightStriker