How to hide column of DataGridView when using custom DataSource?

前端 未结 7 1689
时光取名叫无心
时光取名叫无心 2020-12-08 20:07

I have a small app in c#, it has a DataGridView that gets filled using:

grid.DataSource = MyDatasource array;

MyClass hold the structure for the

7条回答
  •  再見小時候
    2020-12-08 20:35

    In some cases, it might be a bad idea to first add the column to the DataGridView and then hide it.

    I for example have a class that has an NHibernate proxy for an Image property for company logos. If I accessed that property (e.g. by calling its ToString method to show that in a DataGridView), it would download the image from the SQL server. If I had a list of Company objects and used that as the dataSource of the DataGridView like that, then (I suspect) it would download ALL the logos BEFORE I could hide the column.

    To prevent this, I used the custom attribute

     [System.ComponentModel.Browsable(false)]
    

    on the image property, so that the DataGridView ignores the property (doesn't create the column and doesn't call the ToString methods).

     public class Company
     {
         ...
         [System.ComponentModel.Browsable(false)]
         virtual public MyImageClass Logo { get; set;}
    

提交回复
热议问题