2012-02-23 86 views
1

我有一个datagrid,我绑定到类有一些列标记为[Browsable(false)]。这些列显示空白。有什么方法可以解决这个问题。绑定datagridview到[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; 
    } 
} 
+0

一些代码将帮助,列代码... – 2012-02-23 07:29:49

回答