Step1,首先在DataGridView的Columns集合中添加Items,將DataProperityName設置爲泛型集合Items相應屬性的屬性名稱,如Name等,
step 2,編寫代碼:
// 這一句非常重要,是將泛型集合的Items裏沒有綁定的屬性不要顯示在DataGridView中
this.dgv.AutoGenerateColumns = false;
如果將該語註釋掉,效果如下圖,ID自動顯示爲一列
//創建BindingSource對象
BindingSource bind = new BindingSource();//將泛型集合的Values綁定爲BindingSource對象的數據源,很多初學者直接將Dictionary<T>對象直接綁定爲 BindingSource對象的數據源,這是不對的!!!
bind.DataSource = Employee.RecordList.Values;
//將DataGridView的數據源綁定在BindingSource對象上
this.dgv.DataSource = bind;