如果要更改列名,可以通过以下两种方式之一来实现:

  1. 在'UserList'中使用属性来设置列名:

    • 在'UserList'中的属性中,使用'DisplayName'属性来设置列名,如下所示:
      private List<User> _userList;
      public List<User> UserList
      {
          get { return _userList; }
          set
          {
              _userList = value;
              OnPropertyChanged(nameof(UserList));
          }
      }
      
      public class User
      {
          [DisplayName('姓名')]
          public string Name { get; set; }  
          [DisplayName('年龄')]
          public int Age { get; set; }
          // 其他属性
      }
      
    • 然后在'DataGridView'中,将列的'DataPropertyName'属性设置为实际的属性名,如下所示:
      dataGridView1.Columns['Name'].DataPropertyName = 'Name';
      dataGridView1.Columns['Age'].DataPropertyName = 'Age';
      
  2. 在'DataGridView'中手动添加列,并设置列名:

    • 在'DataGridView'的设计视图中,手动添加列,然后设置'HeaderText'属性为想要的列名。

这样,当数据绑定到'DataGridView'时,列名将会显示为你所设置的列名。

C# DataGridView 数据绑定:更改列名

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

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