ListView

作者:追风剑情 发布于:2017-12-28 17:35 分类:C#

示例

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 PackageProject
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            InitListView();
        }

        private void InitListView()
        {
            string[] columnTitles = new string[] { "列标题1", "列标题2", "列标题3" };

            this.listView1.View = View.Details;
            this.listView1.GridLines = true;
            this.listView1.FullRowSelect = true;
            ListViewGroup group1 = new ListViewGroup("分组一");
            ListViewGroup group2 = new ListViewGroup("分组二");
            this.listView1.Groups.Add(group1);
            this.listView1.Groups.Add(group2);
            this.listView1.ShowGroups = true;

            //创建图标列表
            Image icon = Image.FromFile("bagua.jpg");
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(10, 10);
            this.listView1.SmallImageList = imgList;

            //创建列标题
            for (int i = 0; i < columnTitles.Length; i++) {
                this.listView1.Columns.Add(columnTitles[0], 120, HorizontalAlignment.Left);
            }

            //创建列表项
            this.listView1.BeginUpdate();
            for (int i = 0; i < 15; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.ImageIndex = i;
                lvi.Text = "subitem" + i;
                lvi.SubItems.Add("第2列,第" + i + "行");
                lvi.SubItems.Add("第3列,第" + i + "行");
                this.listView1.Items.Add(lvi);

                //分组显示
                if (i < 5)
                    group1.Items.Add(lvi);
                else
                    group2.Items.Add(lvi);

                imgList.Images.Add(icon);
            }
            this.listView1.EndUpdate();
            
        }
    }
}

效果

1111111.png

标签: C#

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号