在 WinForms 中展示一个列表中的数据,可以使用 DataGridView 控件。下面是一个简单的示例代码,演示如何在 WinForms 中展示一个列表中的数据:

  1. 在 WinForms 窗体上放置一个 DataGridView 控件,并设置其名称为 dataGridView1。
  2. 在窗体的 Load 事件中,添加以下代码:
private void Form1_Load(object sender, EventArgs e)
{
    // 创建一个数据源,例如一个 List<T> 对象
    List<Person> personList = new List<Person>();
    personList.Add(new Person('John', 25));
    personList.Add(new Person('Jane', 30));
    personList.Add(new Person('Mike', 35));

    // 将数据源绑定到 DataGridView 控件
    dataGridView1.DataSource = personList;
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

在这个示例中,我们创建了一个名为 Person 的简单类,它具有 Name 和 Age 属性。然后,我们创建了一个包含几个 Person 对象的 List 数据源。最后,我们将数据源绑定到 DataGridView 控件中,这样列表中的数据就会在窗体加载时显示出来。

请注意,在实际的应用程序中,你可能需要从数据库或其他数据源中获取数据,并将其转换为适合绑定到 DataGridView 的格式。

WinForms 数据展示:使用 DataGridView 显示列表数据

原文地址: https://www.cveoy.top/t/topic/kfAE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录