首页 新闻 赞助 找找看

C#使用集合组织相关数据

0
悬赏园豆:30 [待解决问题]

  

  1.如何使用泛型集合List绑定DataGridView 求详细不步骤 举例

 1   private void Form1_Load(object sender, EventArgs e)
 2         {
 3             //建立部门信息集合
 4 
 5             Dictionary<string, SE> engineers = new Dictionary<string, SE>();
 6 
 7 
 8 
 9             ////创建工程师兵初始化赋值
10             SE jack = new SE();
11 
12             jack.Id = "1001";
13             jack.Name = "王小毛";
14             jack.Age = "22";
15             jack.Gender = Gender.男;
16             jack.Popularity = "15";
17 
18 
19             SE rose = new SE();
20 
21             rose.Id = "1002";
22             rose.Name = "李四";
23             rose.Age = "21";
24             rose.Gender = Gender.男;
25             rose.Popularity = "20";
26 
27 
28             SE roro = new SE();
29 
30             roro.Id = "1003";
31             roro.Name = "王五";
32             roro.Age = "18";
33             roro.Gender = Gender.男;
34             roro.Popularity = "10";
35 
36 
37             PM pm = new PM();
38             pm.Age = 50;
39             pm.Gender = Gender.男;
40             pm.Name = "盖茨";
41             pm.Id = "8230";
42 
43             //分别添加元素
44 
45             engineers.Add(jack.Id,jack);
46             engineers.Add(rose.Id,rose);
47             engineers.Add(roro.Id,roro);
48 
49 
50             //创建ComboBox项  运行时确定泛型类型支持的数据类型
51 
52             ComboBoxItem<SE> itemJack = new ComboBoxItem<SE>();
53             itemJack.ItemText = jack.Name;
54             itemJack.ItemValue =jack.ToString();
55 
56 
57             ComboBoxItem<SE> itemJoe = new ComboBoxItem<SE>();
58             itemJoe.ItemText = rose.Name;
59             itemJoe.ItemValue = rose.ToString();
60 
61             List<ComboBoxItem<SE>> items = new List<ComboBoxItem<SE>>();
62             items.Add(itemJack);
63             items.Add(itemJoe);
64 
65 
66             this.CmbEnginners.DataSource = items;
67             this.CmbEnginners.DisplayMember = "ItemText";
68             this.CmbEnginners.DisplayMember = "ItemValue";
69 
70 
71 
72      
73 
74           
75             
76         }
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87   private void CmbEnginners_SelectedIndexChanged(object sender, EventArgs e)
88         {
89             //下拉选项改变
90             if (CmbEnginners.SelectedIndex>0)
91             {
92                 SE se = (SE)CmbEnginners.SelectedValue;//  此处有没有问题  运行报错 说 无法将类型为“Ch02.ComboBoxItem`1[Ch02.SE]”的对象强制转换为类型“Ch02.SE”。
93 
94                 MessageBox.Show(string.Format("工程代号:{0}", se.Age.ToString()));
95             }
96         }
View Code

 

说明

   泛型类绑定ComboBox 在29行行说转型失败 ,但是能运行,结果在ComboBox上显示的内容为Ch02.SE。  求高手指点

  

 

  

☭来一份陆家嘴的主页 ☭来一份陆家嘴 | 初学一级 | 园豆:172
提问于:2015-02-28 20:27
< >
分享
所有回答(1)
0

需要重写ToString方法

jello chen | 园豆:7306 (大侠五级) | 2015-03-01 19:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册