Dictironary 綁定數據源

 

           


          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;






發佈了28 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章