• 接續上一篇C#泛型應用, 今天寫一個實體化泛型類別的物件初始化DataGridView欄位的範例

  • 先貼出畫面結果如下:

image

情況1:

定義 Employee類別如下

   1: public class Employee
   2: {
   3:     public int num;
   4:     public string name;
   5:     public string sex;
   6:     public int age;
   7:     public string birthday;
   8:     public long salary;
   9: }

假設產生兩筆Employee資料

   1: List<Employee> employee = new List<Employee>()
   2: {
   3:   new Employee() { name = "Henery", num = 1, sex = "男" },
   4:   new Employee() { name = "Jane", num = 2,  sex = "女" }
   5:  };

將employee資料餵給dataGridView1

   1: dataGridView1.DataSource = employee;

按下測試Employee發現資料出不來@@

image

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

情況2:  修改情況1無法加入DataGridView的窘境, Employee2欄位名稱個數與Employee一樣, 但這次每個欄位都加入get/set功能

定義 Employee2類別如下

   1: public class Employee2    
   2: {
   3:     public int num { get; set; }
   4:     public string name { get; set; }
   5:     public string sex { get; set; }
   6:     public int age { get; set; }
   7:     public string birthday { get; set; }
   8:     public long salary { get; set; }
   9: }

假設產生兩筆Employee2資料

   1: List<Employee2> employee2 = new List<Employee2>()
   2: {
   3:     new Employee2() { name = "Henery", num = 1, sex = "男" },
   4:     new Employee2() { name = "Jane", num = 2,  sex = "女" }
   5: };

將employee2資料餵給dataGridView1

   1: dataGridView1.DataSource = employee2;

按下<測試Employee2>按鈕, 這次總算可以顯示在dataGridView1上了^_^

image

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

情況3:  仿照情況2, 每個欄位都加入get/set功能, 並且泛型化資料格式

   1: public class Employee<T>
   2: {
   3:     public T num { get; set; }
   4:     public T name { get; set; }
   5:     public T sex { get; set; }
   6:     public T age { get; set; }
   7:     public T birthday { get; set; }
   8:     public T salary { get; set; }
   9: }

假設產生兩筆Employee<T>資料

   1: List<Employee<object>> employee = new List<Employee<object>>()
   2: {
   3:   new Employee<object>() { age = 25, birthday = "1998/8/28", name = "Henery", num = 1, salary = 1000, sex = "男" },
   4:   new Employee<object>() { age = 23, birthday = "2000/5/28", name = "Jane", num = 1, salary = 1000, sex = "女" }
   5:  };

或是用下列Add方法連續加入兩筆資料至List

   1: List<Employee<object>> employee = new List<Employee<object>>();               
   2:   employee.Add(new Employee<object>() { age = 25, birthday = "1998/8/28", name = "Henery", num = 1, salary = 1000, sex = "男" });
   3:   employee.Add(new Employee<object>() { age = 23, birthday = "2000/5/28", name = "Jane", num = 1, salary = 1000, sex = "女" });

接著,將employee資料餵給dataGridView1

   1: dataGridView1.DataSource = employee;

顯示結果如下:

image

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()