【发布时间】:2012-03-13 13:24:30
【问题描述】:
我有一个绑定到类的数据网格,其中一些列标记为[Browsable(false)]。这些列显示为空白。有什么办法可以关闭它。
我没有使用任何自动绑定,而是自己创建列并设置 DataPropertyName 属性。
我花了很长时间才找到为什么它们现在是空白的,我希望有一些简单的方法可以显示它们中的值。我唯一能想到的就是编写自己的 DataGridViewColumn 类并重新实现绑定。还有其他想法吗?
这是一些演示问题的代码,GridBrowsableProblem 是一个新表单,其中放置了一个 DataGridView。运行时 ProblemProperty 没有值。
public partial class GridBrowsableProblem : Form
{
public class ProblemTestClass
{
[Browsable(false)]
public string ProblemProperty { get; set; }
public string AnotherProperty { get; set; }
}
public GridBrowsableProblem()
{
InitializeComponent();
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.DataPropertyName = "ProblemProperty";
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
column2.DataPropertyName = "AnotherProperty";
ProblemTestClass item = new ProblemTestClass();
item.ProblemProperty = "test1";
item.AnotherProperty = "test2";
BindingList<ProblemTestClass> bindingList = new BindingList<ProblemTestClass>();
bindingList.Add(item);
dataGridView1.Columns.Add(column1);
dataGridView1.Columns.Add(column2);
dataGridView1.DataSource = bindingList;
}
}
【问题讨论】:
-
一些代码会有所帮助,列代码...
标签: .net winforms binding .net-3.5 datagridview