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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With