码迷,mamicode.com
首页 > 其他好文 > 详细

viewlist控件的简单应用

时间:2016-06-30 08:41:26      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1     class student
2     {
3         public string _code { get; set; }
4         public string _name { get; set; }
5         public bool _sex { get; set; }
6         public DateTime _bithday { get; set; }
7         public decimal _score { get; set; }
8 
9     }
 1  public partial class Form1 : Form
 2     {
 3         SqlConnection conn = null;
 4         SqlCommand cmd = null;
 5         public Form1()
 6         {
 7             InitializeComponent();
 8             conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");
 9             cmd = conn.CreateCommand();
10         }
11 
12         private void listView1_SelectedIndexChanged(object sender, EventArgs e)
13         {
14             
15  
16         }
17 
18         private void button1_Click(object sender, EventArgs e)
19         {
20             List<student> list = new List<student>();
21             cmd.CommandText = "select * from student";
22             conn.Open();
23             SqlDataReader dr = cmd.ExecuteReader();
24             if(dr.HasRows)
25             {
26                 while(dr.Read())
27                 {
28                     student s = new student();
29                     s._code = dr[0].ToString();
30                     s._name = dr[1].ToString();
31                     s._sex = Convert.ToBoolean(dr[2]);
32                     s._bithday = Convert.ToDateTime(dr[3]);
33                     s._score = Convert.ToDecimal(dr[4]);
34 
35                     list.Add(s);
36 
37                 }
38             }
39 
40             conn.Close();
41 
42             foreach(student a in list)
43             {
44                 ListViewItem li = new ListViewItem();
45                 li.Text = a._code;
46 
47                 li.SubItems.Add(a._name);
48                 li.SubItems.Add((a._sex ? "" : ""));
49                 li.SubItems.Add(a._bithday.ToString("yyyy年MM月dd日"));
50                 li.SubItems.Add(a._score.ToString());
51 
52                 listView1.Items.Add(li);
53             }
54 
55         }
56     }

 

viewlist控件的简单应用

标签:

原文地址:http://www.cnblogs.com/mazhijie/p/5628802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!