Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DataGridView to a List<>, some properties Should not be shown

I am trying to bind a DataGridView to a List, where MyObject looks like

class MyObject
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

//List<MyObject> objects;
grid.Columns[0].DataPropertyName = "Property1";
grid.DataSource = objects;

I want only one property to be displayed, but instead I get another column added to my DataGridView where the Property2 is also displayed. How can I prevent it from being added?

like image 505
Dmitry Risenberg Avatar asked May 22 '09 21:05

Dmitry Risenberg


2 Answers

If you never want that property displayed:

class MyObject
{
    public string Property1 { get; set; }
    [Browsable(false)]
    public string Property2 { get; set; }
}

Otherwise, as already stated - set AutoGenerateColumns to false and add them manually.

like image 55
Marc Gravell Avatar answered Oct 22 '22 05:10

Marc Gravell


It sounds like you have the AutoGenerateColumns property of your DataGridView control set to True. You can either set it to False, or use the .Columns.Remove method to remove the column you don't want to see.

like image 38
Robert Harvey Avatar answered Oct 22 '22 03:10

Robert Harvey