2015年8月1日 星期六

ListView增加欄位

1. 選擇控制項"ListView",使用滑鼠拖拉至[設計]。

2. 程式設計如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //Table initial set
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            //Table item(row)
            listView1.Columns.Add("NAME", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("TYPE", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("PRICE", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("QUANTITY", 80, HorizontalAlignment.Center);
            //Add table value
            ListViewItem item0 = new ListViewItem();
            item0.SubItems.Clear();
            item0.SubItems[0].Text = "Sample" + 1.ToString(); //NAME
            item0.SubItems.Add("A".ToString()); //TYPE
            item0.SubItems.Add(10.ToString());  //PRICE
            item0.SubItems.Add(123.ToString()); //QUANTITY
            listView1.Items.Add(item0);
            ListViewItem item1 = new ListViewItem();
            item1.SubItems.Clear();
            item1.SubItems[0].Text = "Sample" + 2.ToString(); //NAME
            item1.SubItems.Add("B".ToString()); //TYPE
            item1.SubItems.Add(13.ToString());  //PRICE
            item1.SubItems.Add(153.ToString()); //QUANTITY
            listView1.Items.Add(item1);
        }
    }
}
3. 執行結果:

沒有留言:

張貼留言