How do I set columnNames for a dataGridView bound to a List<T>?

删除回忆录丶 提交于 2019-11-30 21:40:40

This works..

using System.ComponentModel;

List<equip> EquipList = new List<equip>();

DataGridView.Datasource = EquipList;

below:the first two properties in the class will be displayed with with their new names, the third property will not display as per the Browsable attribute.

public class equip
  {
   [DisplayName("Equipment ID")]
   public int EquipID {get;set;}

   [DisplayName("Equipment Name")]
   public string EquipName {get;set;}

   [Browsable(false)]
   public int recordId {get; private set;}
  }

You can try this

DataGridViewTextBoxColumn dt = new DataGridViewTextBoxColumn();
dt.DataPropertyName = "EquipName"; //Property to bind.
dt.HeaderText = "Name";
dataGridView1.Columns.Add(dt);

You can achieve this easily with Designer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!