关于属性控件PropertyGrid的问题 用PropertyGrid控件在运行时显示组件属性时,怎样让属性为空值的时候不显示在PropertyGrid中。能否不按控件自身提供的分类排序和字母排序,自定义PropertyGrid中各组的排序? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [Category()]可以自定义属性的分类。为空不显示是不可能的,除非你自己扩展PropertyGrid控件 public partial class Form1 : Form{ public Form1() { InitializeComponent(); this.propertyGrid1.ToolbarVisible = false; this.propertyGrid1.PropertySort = PropertySort.Categorized; this.propertyGrid1.SelectedObject = new My() { FirstName = "ShowMe", LastName = "MeToo" }; }}[TypeConverter( typeof(My.OrderedPropertyConverter) )]public class My{ public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } private class OrderedPropertyConverter : TypeConverter { public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { List<PropertyDescriptor> result = new List<PropertyDescriptor>(); foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(value, attributes)) { // 不显示null的属性 - 不过也就没有办法编辑了:) if (pd.PropertyType.IsValueType || pd.GetValue(value) != null) { result.Add(pd); } } PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(result.ToArray()); // 自定义排序 pdc.Sort(new string[]{"FirstName", "LastName", "Age"} ); return pdc; } }} propertyGrid.Refresh();刷新一下就可以了 - - 在win7下无法运行gacutil/i datatable 求交集 SQL问题,知道的说下,谢谢。 高分问:设计时获得当前项目执行文件路径 如何解决WindowService程序中的死循环问题/? 窗体问题 一个登录窗口的问题! 请问Longhorn操作系统默认安装.NET Framework吗? 编辑的时候 获取文本框里的值,值为什么还是没变? 在winform中,能实现的显示两个表关联数据(即点+号显示另一数据库表数据), 在webform中能实现吗?如何写呢? C# 去掉标题栏后,这样用鼠标移动窗体? 把C语言代码翻译成C#语言
可以自定义属性的分类。为空不显示是不可能的,除非你自己扩展PropertyGrid控件
{
public Form1()
{
InitializeComponent(); this.propertyGrid1.ToolbarVisible = false;
this.propertyGrid1.PropertySort = PropertySort.Categorized;
this.propertyGrid1.SelectedObject = new My() { FirstName = "ShowMe", LastName = "MeToo" };
}
}[TypeConverter( typeof(My.OrderedPropertyConverter) )]
public class My
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; } private class OrderedPropertyConverter : TypeConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
List<PropertyDescriptor> result = new List<PropertyDescriptor>();
foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(value, attributes))
{
// 不显示null的属性 - 不过也就没有办法编辑了:)
if (pd.PropertyType.IsValueType || pd.GetValue(value) != null)
{
result.Add(pd);
}
}
PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(result.ToArray()); // 自定义排序
pdc.Sort(new string[]{"FirstName", "LastName", "Age"} );
return pdc;
}
}
}