资源收集
C#winform中ListView的使用
C# WinForm开发系列 - ListBox/ListView/Panel(介绍了一些listview的高级用法)
直接上代码
示例一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
按details显示为图一,listView1.View = View.LargeIcon显示为图二
示例二
代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DirectoryInfo dir = new DirectoryInfo(@"D: est"); 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;
显示结果