views:

1146

answers:

3

I think there must be an attribute to hide a public property from the datagridview. But I can't find it.

+5  A: 

If you are adding the columns yourself... don't add the columns you don't want.

If you have AutoCreateColumns enabled, then:

  • if it is a class-based model, add [Browsable(false)] to properties you don't want
  • or set the column's .Visible to false
  • or simply remove the columns you don't want afterwards
Marc Gravell
A: 

From your question, I would imagine you don't want to show certain "columns" within the datagridview? If so, use the Columns property to add and remove any automatically created columns that are found on the datasource which you use to attach to the grid.

The DataGridView by default will create columns for all public properties on the underlying data source object. So,

public class MyClass
{
   private string _name;

   public string Name
   {
      get{ return _name; }
      set { _name = value; }
   }

   public string TestProperty
   {
      { get { return "Sample"; }
   }
}

...
[inside some form that contains your DataGridView class]

MyClass c = new MyClass();

// setting the data source will generate a column for "Name" and "TestProperty"
dataGridView1.DataSource = c;

// to remove specific columns from the DataGridView
// dataGridView1.Columns.Remove("TestProperty")
Mike J