ListView

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

示例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace PackageProject
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. InitListView();
  19. }
  20.  
  21. private void InitListView()
  22. {
  23. string[] columnTitles = new string[] { "列标题1", "列标题2", "列标题3" };
  24.  
  25. this.listView1.View = View.Details;
  26. this.listView1.GridLines = true;
  27. this.listView1.FullRowSelect = true;
  28. ListViewGroup group1 = new ListViewGroup("分组一");
  29. ListViewGroup group2 = new ListViewGroup("分组二");
  30. this.listView1.Groups.Add(group1);
  31. this.listView1.Groups.Add(group2);
  32. this.listView1.ShowGroups = true;
  33.  
  34. //创建图标列表
  35. Image icon = Image.FromFile("bagua.jpg");
  36. ImageList imgList = new ImageList();
  37. imgList.ImageSize = new Size(10, 10);
  38. this.listView1.SmallImageList = imgList;
  39.  
  40. //创建列标题
  41. for (int i = 0; i < columnTitles.Length; i++) {
  42. this.listView1.Columns.Add(columnTitles[0], 120, HorizontalAlignment.Left);
  43. }
  44.  
  45. //创建列表项
  46. this.listView1.BeginUpdate();
  47. for (int i = 0; i < 15; i++)
  48. {
  49. ListViewItem lvi = new ListViewItem();
  50. lvi.ImageIndex = i;
  51. lvi.Text = "subitem" + i;
  52. lvi.SubItems.Add("第2列,第" + i + "行");
  53. lvi.SubItems.Add("第3列,第" + i + "行");
  54. this.listView1.Items.Add(lvi);
  55.  
  56. //分组显示
  57. if (i < 5)
  58. group1.Items.Add(lvi);
  59. else
  60. group2.Items.Add(lvi);
  61.  
  62. imgList.Images.Add(icon);
  63. }
  64. this.listView1.EndUpdate();
  65. }
  66. }
  67. }

效果

1111111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号