listView控件应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication7
{
    class student
    {
        public string _code { get; set; }
        public string _name { get; set; }
        public bool _sex { get; set; }
        public DateTime _bithday { get; set; }
        public decimal _score { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public Form1()
        {
            InitializeComponent();
            conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");
            cmd = conn.CreateCommand();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<student> list = new List<student>();
            cmd.CommandText = "select * from student";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if(dr.HasRows)
            {
                while(dr.Read())
                {
                    student s = new student();
                    s._code = dr[0].ToString();
                    s._name = dr[1].ToString();
                    s._sex = Convert.ToBoolean(dr[2]);
                    s._bithday = Convert.ToDateTime(dr[3]);
                    s._score = Convert.ToDecimal(dr[4]);

                    list.Add(s);

                }
            }

            conn.Close();

            foreach(student a in list)
            {
                ListViewItem li = new ListViewItem();
                li.Text = a._code;

                li.SubItems.Add(a._name);
                li.SubItems.Add((a._sex ? "" : ""));
                li.SubItems.Add(a._bithday.ToString("yyyy年MM月dd日"));
                li.SubItems.Add(a._score.ToString());

                listView1.Items.Add(li);
            }

        }
    }
}

posted @ 2016-06-28 16:46  枫炎  阅读(357)  评论(0编辑  收藏  举报