datagridview显示绑定数据源的某几个属性的方法

给datagridview绑定了一个List<T>的数据源,但是我仅仅想显示class T中的某几个属性怎么办,在网上查到了一下几种方法:

方法一:

设置

 

AutoGenerateColumns = false;

手动添加列,并把列的DataPropertyName属性设置为源中的类的对应属性的名

 

方法二:

将类中不想显示的性加上属性(attitude):

      [System .ComponentModel .Browsable (false )]

例如,我不想显示a属性,可以如此:

      [System.ComponentModel.Browsable(false)]
public int a
{
get;set;
}

这样,所有绑定T的为源来显示的控件,都将不显示T中的a属性了。

方法三:

用linq新建一个集合:

var q = from l in list 
select new
{
l.a,
l.b,
l.c
};
this.dataGridView1.DataSource = q.ToList();

new里面仅仅包含你想显示的属性。

 

三种方法各有优劣,在实际中视情况使用,假如有其他方法,以后再加。

2012年1月11日21:49:02

方法四:

    //等绑定数据之后,再移除不要显示的列
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = customersDataSet;
dataGridView1.Columns.Remove("Fax");
posted @ 2012-01-05 23:42  果壳中的宇宙  阅读(2127)  评论(0编辑  收藏  举报