Tôi có một lưới thuộc tính với 2 mục. Quốc gia & Thành phố. Tôi có 1 bảng trong cơ sở dữ liệu: CountryCityTable lưu LocationId, Title, ParentId. Đối với các quốc gia parentId = 0 và cho các thành phố là countryid.Làm thế nào tôi có thể cập nhật các giá trị thuộc tính itemgrid khi một mục khác thay đổi trong winform C#?
Trong thuộc tính của tôi, tôi sử dụng chúng và hiển thị trong 2 mục combobox. Xin vui lòng xem mã của tôi:
namespace ProGrid
{
public class KeywordProperties
{
[TypeConverter(typeof(CountryLocationConvertor))]
public string CountryNames { get; set; }
[TypeConverter(typeof(CityLocationConvertor))]
public string CityNames { get; set; }
}
}
Và
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;
}
}
}
Và
KeywordProperties Kp = new KeywordProperties();
myPropertyGrid.SelectedObject = Kp;
Bây giờ, tôi muốn khi người dùng thay đổi tiêu đề nước trong PropertyGrid, Danh sách các thành phố cập nhật (chỉ hiển thị các thành phố mà ParentID trong số này = countryid).
Ngoài ra, trong lớp học, làm cách nào tôi có thể thay đổi Số 20 trong mã của mình (Db.LoadLocations (1,20);) sang id quốc gia đã chọn?
Cảm ơn.
Áp dụng thuộc tính [RefreshProperties]. –
Vui lòng thay đổi mã của tôi với thuộc tính này. Cảm ơn. –
Giống như Hans cho biết, tài sản này hoạt động độc đáo và rất sạch sẽ. Ngoài ra, hãy kiểm tra chi tiết hơn http://stackoverflow.com/a/4955653/586754 –