winform listview用法

资源收集

C#winform中ListView的使用

C# WinForm开发系列 - ListBox/ListView/Panel(介绍了一些listview的高级用法)

 

直接上代码

示例一:

   this.listView1.GridLines = true; //显示表格线
            this.listView1.View = View.Details;//显示表格细节
            this.listView1.LabelEdit = true; //是否可编辑,ListView只可编辑第一列。
            this.listView1.Scrollable = true;//有滚动条
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
            this.listView1.FullRowSelect = true;//是否可以选择行
            //this.listView1.HotTracking = true;// 当选择此属性时则HoverSelection自动为true和Activation属性为oneClick
            //this.listView1.HoverSelection = true;
            //this.listView1.Activation = ItemActivation.Standard; //
            //添加表头
            this.listView1.Columns.Add("列0", 50);
            this.listView1.Columns.Add("列1", 80);
            this.listView1.Columns.Add("列2", 160);
            //添加各项
            ListViewItem[] p = new ListViewItem[4];
            p[0] = new ListViewItem(new string[] { "1", "aaaa", "bbbb" });
            p[1] = new ListViewItem(new string[] { "2", "bbbb", "ggggg" });
            p[2] = new ListViewItem(new string[] { "3", "cccc", "dafdf" });
            p[3] = new ListViewItem(new string[] { "4", "dddd", "32532" });
            //p[0].SubItems[0].BackColor = Color.Red; //用于设置某行的背景颜色

            this.listView1.Items.AddRange(p);
            //也可以用this.listView1.Items.Add();不过需要在使用的前后添加Begin... 和End...防止界面自动刷新
            // 添加分组
            this.listView1.Groups.Add(new ListViewGroup("第一组"));
            this.listView1.Groups.Add(new ListViewGroup("第二组"));

            this.listView1.Items[0].Group = this.listView1.Groups[0];
            this.listView1.Items[1].Group = this.listView1.Groups[0];
            this.listView1.Items[2].Group = this.listView1.Groups[1];
            this.listView1.Items[3].Group = this.listView1.Groups[1];

            listView1.View = View.Details;
View Code

按details显示为图一,listView1.View = View.LargeIcon显示为图二

示例二

代码

 
            DirectoryInfo dir = new DirectoryInfo(@"D:\test");
            DirectoryInfo[] dirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();

            //把子文件夹信息添加到ListView中显示
            foreach (DirectoryInfo directoryInfo in dirs)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Tag = directoryInfo.FullName;
                lvi.Text = directoryInfo.Name;//显示名称
                lvi.ImageIndex = 0;
                // 文件夹的名称属性项
                lvi.SubItems[0].Tag = lvi.Tag;
                lvi.SubItems[0].Text = lvi.Text;
                //文件夹大小属性项
                ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Tag = "文件夹大小TAG";
                lvsi.Text = "文件夹大小TEXT";
                lvi.SubItems.Add(lvsi);
                //修改日期属性项
                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Tag = "修改日期属性项TAG";
                lvsi.Text = "修改日期属性项TAG";
                lvi.SubItems.Add(lvsi);
                //添加文件夹
                this.listView1.Items.Add(lvi);
            }
            //把文件夹下文件信息添加到ListView中显示
            foreach (FileInfo fileInfo in files)
            {
                ListViewItem lvi = new ListViewItem();//文件项
                lvi.Tag = fileInfo.FullName;
                lvi.Text = fileInfo.Name;
                lvi.ImageIndex = 1;
                //文件的名称属性项
                lvi.SubItems[0].Tag = lvi.Tag;
                lvi.SubItems[0].Text = lvi.Text;
                //文件大小属性项
                ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Tag = "文件大小属性项TAG";
                lvsi.Text = "文件大小属性项TEXT";
                lvi.SubItems.Add(lvsi);
                //修改日期属性项
                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Tag = "修改日期属性项TAG";
                lvsi.Text = "修改日期属性项TEXT";
                lvi.SubItems.Add(lvsi);
                //添加文件
                this.listView1.Items.Add(lvi);
            }


            ///必须要有columnheader,不然不会显示
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "文件名";
            ch.Width = 150;
            listView1.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Width = 70;
            ch.Text = "大小";
            listView1.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "修改日期";
            ch.Width = 140;
            listView1.Columns.Add(ch);

            listView1.View = View.Details;
View Code

显示结果

转载于:https://www.cnblogs.com/shengyu-kmust/p/4429104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值