程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#窗體 LISTVIEW,

C#窗體 LISTVIEW,

編輯:C#入門知識

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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved