Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#listview

作者:◆丶依然如风   发布日期:2025-10-05   浏览:50

using System;
using System.Windows.Forms;

namespace ListViewExample
{
    public class ListViewDemo : Form
    {
        private ListView listView1;

        public ListViewDemo()
        {
            // 初始化ListView控件
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.GridLines = true;

            // 添加列
            listView1.Columns.Add("Column 1", 100);
            listView1.Columns.Add("Column 2", 100);
            listView1.Columns.Add("Column 3", 100);

            // 添加行
            ListViewItem item1 = new ListViewItem(new string[] { "Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3" });
            ListViewItem item2 = new ListViewItem(new string[] { "Row 2 Col 1", "Row 2 Col 2", "Row 2 Col 3" });
            ListViewItem item3 = new ListViewItem(new string[] { "Row 3 Col 1", "Row 3 Col 2", "Row 3 Col 3" });

            listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });

            // 将ListView添加到窗体
            this.Controls.Add(listView1);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ListViewDemo());
        }
    }
}

解释说明:

  1. 初始化ListView控件

    • listView1 是一个 ListView 控件,我们将其停靠方式设置为 DockStyle.Fill,使其充满整个窗体。
    • View 属性设置为 Details,表示以详细视图显示数据,并且启用网格线。
  2. 添加列

    • 使用 Columns.Add 方法添加三列,每列指定宽度为 100 像素。
  3. 添加行

    • 创建多个 ListViewItem 对象,每个对象包含三列的数据。
    • 使用 Items.AddRange 方法将这些项添加到 ListView 中。
  4. 将ListView添加到窗体

    • 最后,将 listView1 添加到窗体的控件集合中。
  5. 主程序入口

    • Main 方法是程序的入口点,创建并运行 ListViewDemo 窗体。

上一篇:c# 读取ini配置文件

下一篇:c# datatable排序

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站