Dar*_*man 6 c# propertygrid datagridview properties hide
假设我有一个我希望在DataGridView中显示的属性,但是当PropertyGrid中显示相同的对象时却没有.我知道我可以使用[Browsable(false)],但是在两个视图中都隐藏了它.我也可以做一个gridView.Columns["blah"].Visible = false;,但这与我想要的相反,因为它隐藏在DataGridView中而不是隐藏在PropertyGrid中.有没有办法反过来?(创建一个全新的数据表只是持有相同的数据减去一个字段,并且,与其重新绑定所有的短-这是一个真正的杂牌组装电脑的方式来做事.)或者,我可以用它添加一列到DataGridView解决生活在实际课程中不存在.
可以通过使用PropertyGrid的BrowsableAttributes属性来解决此问题.首先,创建一个这样的新属性:
public class PropertyGridBrowsableAttribute : Attribute
{
private bool browsable;
public PropertyGridBrowsableAttribute(bool browsable){
this.browsable = browsable;
}
}
Run Code Online (Sandbox Code Playgroud)
然后将此属性添加到要在PropertyGrid中显示的所有属性:
[DisplayName("First Name"), Category("Names"), PropertyGridBrowsable(true)]
public string FirstName {
get { return ... }
set { ... }
}
Run Code Online (Sandbox Code Playgroud)
然后像这样设置BrowsableAttributes属性:
myPropertyGrid.BrowsableAttributes = new AttributeCollection(
new Attribute[] { new PropertyGridBrowsableAttribute(true) });
Run Code Online (Sandbox Code Playgroud)
这将仅显示属性网格中的属性属性,并且DataGridView仍然可以访问所有属性,只需更多的编码工作.
我仍然会使用Tergiver并将此行为称为错误,因为Browsable属性的文档明确指出它仅用于属性窗口.
(信用证转到用户"maro",网址为http://www.mycsharp.de/wbb2/thread.php?postid=234565)
| 归档时间: |
|
| 查看次数: |
1925 次 |
| 最近记录: |