【发布时间】:2012-11-18 12:25:39
【问题描述】:
我有一个包含 2 个项目的属性网格。国家和城市。我在数据库中有 1 个表:保存 LocationId、Title、ParentId 的 CountryCityTable。对于国家,parentId = 0,对于城市,是 countryid。
在我的 propertygrid 中,我使用这些并显示在 2 个组合框项目中。请看我的代码:
namespace ProGrid
{
public class KeywordProperties
{
[TypeConverter(typeof(CountryLocationConvertor))]
public string CountryNames { get; set; }
[TypeConverter(typeof(CityLocationConvertor))]
public string CityNames { get; set; }
}
}
和
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;
}
}
}
还有
KeywordProperties Kp = new KeywordProperties();
myPropertyGrid.SelectedObject = Kp;
现在,我希望当用户在 propertygrid 中更改国家/地区标题时,更新城市列表(仅显示这些的 parentid = countryid 的城市)。
另外,在我的课堂上,我如何将代码中的数字 20(Db.LoadLocations(1,20);) 更改为选定的国家/地区 ID?
谢谢。
【问题讨论】:
-
应用 [RefreshProperties] 属性。
-
请使用此属性更改我的代码。谢谢。
-
正如汉斯所说,这家酒店运作良好,非常干净。另请检查更多详细信息stackoverflow.com/a/4955653/586754
标签: c# propertygrid