C#窗体 LISTVIEW

来源:转载

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApplication1 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form1_Load(object sender, EventArgs e) 21 { 22 23 24 //设置该listview关联的imagelist 25 listView1.LargeImageList = imageList2; 26 listView1.SmallImageList = imageList1; 27 28 //显示详细信息 29 listView1.View = View.Details; 30 31 //添加组 32 ListViewGroup lg1 = new ListViewGroup(); 33 lg1.Header = "一组"; 34 lg1.Name = "one"; 35 listView1.Groups.Add(lg1); 36 ListViewGroup lg2 = new ListViewGroup(); 37 lg2.Header = "二组"; 38 lg2.Name = "two"; 39 listView1.Groups.Add(lg2); 40 41 //添加列 42 listView1.Columns.Add("代号"); 43 listView1.Columns.Add("姓名"); 44 listView1.Columns.Add("生日"); 45 46 //选中整行 47 listView1.FullRowSelect = true; 48 49 //显示checkbox 50 listView1.CheckBoxes = true; 51 52 //添加项,listview每一条数据称为一项 53 InfoDA da = new InfoDA(); 54 List<Info> list = da.Select(); 55 56 foreach (Info data in list) 57 { 58 //造主项 59 ListViewItem zhuxiang = new ListViewItem(); 60 zhuxiang.Text = data.Code; 61 zhuxiang.ImageIndex = 0; 62 zhuxiang.Group = listView1.Groups[0]; 63 64 //造子项 65 ListViewItem.ListViewSubItem zi1 = new ListViewItem.ListViewSubItem(); 66 zi1.Text = data.Name; 67 zhuxiang.SubItems.Add(zi1); 68 69 ListViewItem.ListViewSubItem zi2 = new ListViewItem.ListViewSubItem(); 70 zi2.Text = data.Birthday.ToString("yyyy-MM-dd"); ; 71 zhuxiang.SubItems.Add(zi2); 72 73 74 //添加项 75 listView1.Items.Add(zhuxiang); 76 77 } 78 79 80 foreach (Info data in list) 81 { 82 //造主项 83 ListViewItem zhuxiang = new ListViewItem(); 84 zhuxiang.Text = data.Code; 85 zhuxiang.ImageIndex = 1; 86 zhuxiang.Group = listView1.Groups[1]; 87 88 //造子项 89 ListViewItem.ListViewSubItem zi1 = new ListViewItem.ListViewSubItem(); 90 zi1.Text = data.Name; 91 zhuxiang.SubItems.Add(zi1); 92 93 ListViewItem.ListViewSubItem zi2 = new ListViewItem.ListViewSubItem(); 94 zi2.Text = data.Birthday.ToString("yyyy-MM-dd"); ; 95 zhuxiang.SubItems.Add(zi2); 96 97 98 //添加项 99 listView1.Items.Add(zhuxiang);100 101 }102 103 104 }105 106 private void button1_Click(object sender, EventArgs e)107 {108 listView1.View = View.Details;109 }110 111 private void button2_Click(object sender, EventArgs e)112 {113 listView1.View = View.LargeIcon;114 }115 116 private void button3_Click(object sender, EventArgs e)117 {118 listView1.View = View.SmallIcon;119 }120 121 private void button4_Click(object sender, EventArgs e)122 {123 if (listView1.SelectedItems.Count > 0)124 {125 MessageBox.Show(listView1.SelectedItems[0].Text);126 }127 }128 129 private void button5_Click(object sender, EventArgs e)130 {131 //listView1.CheckBoxes = true;132 }133 134 private void button6_Click(object sender, EventArgs e)135 {136 string s = "";137 138 foreach (ListViewItem item in listView1.CheckedItems)139 {140 s += item.Text+"--";141 }142 143 MessageBox.Show(s);144 }145 }146 }

分享给朋友:
您可能感兴趣的文章:
随机阅读: