Mam siatkę własności z 2 przedmiotami. Kraj & Miasta. Mam 1 tabela w bazie danych: CountryCityTable, które zapisują LocationId, Title, ParentId. Dla krajów parentId = 0 i dla miast jest countryid.Jak mogę zaktualizować wartości właściwości propertygrid po zmianie innego elementu w winform C#?
Na mojej liście właściwości, używam ich i pokazuję w 2 pozycjach combobox. Proszę zobaczyć mój kod:
namespace ProGrid
{
public class KeywordProperties
{
[TypeConverter(typeof(CountryLocationConvertor))]
public string CountryNames { get; set; }
[TypeConverter(typeof(CityLocationConvertor))]
public string CityNames { get; set; }
}
}
I
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;
}
}
}
I
KeywordProperties Kp = new KeywordProperties();
myPropertyGrid.SelectedObject = Kp;
Teraz chcę, kiedy użytkownik zmienił tytuł kraju, w PropertyGrid, Lista miast aktualizowana (tylko wyświetlanie miast, które parentId z tych = countryid).
Ponadto, w mojej klasie, jak mogę zmienić numer 20 w moim kodzie (Db.LoadLocations (1,20);) na wybrany identyfikator kraju?
Dziękuję.
Zastosuj atrybut [RefreshProperties]. –
Proszę zmienić mój kod za pomocą tego atrybutu. Dzięki. –
Tak jak powiedział Hans, ta nieruchomość działa ładnie i jest bardzo czysta. Sprawdź również więcej szczegółów http://stackoverflow.com/a/4955653/586754 –