虽然现在winform开发很少使用微软自带的控件,但其中有一个控件还是不错的,它就是ListView控件。操作系统的文件夹页,就是ListView控件的样子,数据展示包括大图标、小图标、列表、明细等。今天通过一个实例,来展示下这个控件是如何使用的?先看下效果图
这个实例实现了以下功能:
- 1、数据分组展示
- 2、行最前面增加了图标
- 3、整行全部选中
- 4、数据展示模式可切换
代码部分也不复杂,这里是全部代码的:创建一个Form页面,里面放一个ToolStrip的工具栏和一个ListView控件即可
private void tbtnLoadData_Click(object sender, EventArgs e) { #region 加载ListView控件,分组显示 this.listview1.Groups.Clear(); this.listview1.Items.Clear(); this.listview1.View = View.Details; this.listview1.FullRowSelect = true; ColumnHeader columnHeader0 = new ColumnHeader(); columnHeader0.Text = "Title"; columnHeader0.Width = 250; ColumnHeader columnHeader1 = new ColumnHeader(); columnHeader1.Text = "Author"; columnHeader1.Width = 150; ColumnHeader columnHeader2 = new ColumnHeader(); columnHeader2.Text = "Year"; columnHeader2.Width = 50; if (this.listview1.Columns.Count < 3) { this.listview1.Columns.AddRange(new ColumnHeader[] { columnHeader0, columnHeader1, columnHeader2 }); } ListViewGroup group1 = new ListViewGroup("001"); ListViewGroup group2 = new ListViewGroup("002"); this.listview1.Groups.Add(group1); this.listview1.Groups.Add(group2); ListViewItem item0 = new ListViewItem(new string[] { "Programming Windows", "Petzold, Charles", "1998" }, 0, group1); ListViewItem item1 = new ListViewItem(new string[] { "Code: The Hidden Language of Computer Hardware and Software", "Petzold, Charles", "2000" }, 0, group1); ListViewItem item2 = new ListViewItem(new string[] { "Programming Windows with C#", "Petzold, Charles", "2001" }, 0, group1); ListViewItem item3 = new ListViewItem(new string[] { "Coding Techniques for Microsoft Visual Basic .NET", "Connell, John", "2001" }, 1, group2); ListViewItem item4 = new ListViewItem(new string[] { "C# for Java Developers", "Jones, Allen & Freeman, Adam", "2002" }, 1, group2); ListViewItem item5 = new ListViewItem(new string[] { "Microsoft .NET XML Web Services Step by Step", "Jones, Allen & Freeman, Adam", "2002" }, 1, group2); this.listview1.Items.AddRange(new ListViewItem[] { item0, item1, item2, item3, item4, item5 }); //加上图标 this.listview1.LargeImageList = this.imageList1; this.listview1.SmallImageList = this.imageList1; int index = 0; foreach(ListViewItem lvi in this.listview1.Items) { lvi.ImageIndex = index; index++; } #endregion } private void tbtnLarge_Click(object sender, EventArgs e) { this.listview1.View = View.LargeIcon; } private void tbtnSmall_Click(object sender, EventArgs e) { this.listview1.View = View.SmallIcon; } private void tbtnDetail_Click(object sender, EventArgs e) { this.listview1.View = View.Details; }
相关阅读
文章评论