标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _2015_1_25 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public DataTable getData() { //构建表 DataTable dt = new DataTable(); //向表中添加列 dt.Columns.Add("code"); dt.Columns.Add("name"); dt.Columns.Add("nianling"); dt.Columns.Add("fenshu"); //添加行 DataRow dr1 = dt.NewRow(); dr1["code"] = "101"; dr1["name"] = "张三"; dr1["nianling"] = "22"; dr1["fenshu"] = "98"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["code"] = "102"; dr2["name"] = "李四"; dr2["nianling"] = "23"; dr2["fenshu"] = "97"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3["code"] = "103"; dr3["name"] = "王五"; dr3["nianling"] = "20"; dr3["fenshu"] = "88"; dt.Rows.Add(dr3); return dt; } private void Form1_Load(object sender, EventArgs e) { //DataTable dt = getData(); listView1.Columns.Add("学号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("年龄"); listView1.Columns.Add("分数"); ListViewItem ivt1 = new ListViewItem(); ivt1.Text = "101"; ivt1.SubItems.Add ("张三"); ivt1.SubItems.Add("22"); ivt1.SubItems.Add("97"); listView1.Items.Add(ivt1); } public void ShouDongTianchong() { ListViewItem lvt1 = new ListViewItem(); lvt1.Text = "101"; lvt1.SubItems.Add("张三"); lvt1.SubItems.Add("22"); lvt1.SubItems.Add("98"); listView1.Items.Add(lvt1); ListViewItem lvt2 = new ListViewItem(); lvt2.Text = "1012"; lvt2.SubItems.Add("张三2"); lvt2.SubItems.Add("222"); lvt2.SubItems.Add("982"); listView1.Items.Add(lvt2); } private void button1_Click(object sender, EventArgs e) { ShouDongTianchong(); } private void button2_Click(object sender, EventArgs e) { DataTable dt = getData();//读取整个datatable DataRow dr1 = dt.Rows[0];//获取dt的第一行 //创建listview的第一行的项 ListViewItem lt1 = new ListViewItem(); lt1.Text = dr1["code"].ToString(); lt1.SubItems.Add(dr1["name"].ToString()); lt1.SubItems.Add(dr1["nianling"].ToString()); lt1.SubItems.Add(dr1["fenshu"].ToString()); listView1.Items.Add(lt1); } private void button3_Click(object sender, EventArgs e) { DataTable dt = getData();//读取整个datatable for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr1 = dt.Rows[i];//获取dt的第一行 //创建listview的第一行的项 ListViewItem lt1 = new ListViewItem(); lt1.Text = dr1["code"].ToString(); lt1.SubItems.Add(dr1["name"].ToString()); lt1.SubItems.Add(dr1["nianling"].ToString()); lt1.SubItems.Add(dr1["fenshu"].ToString()); listView1.Items.Add(lt1); } } } }
标签:
原文地址:http://www.cnblogs.com/XMH1217423419/p/4298638.html