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

烈酒焚心 提交于 2019-11-30 17:39:04

问题


As shown below I have created a List of equip.I would like to have the column names more descriptive - is there an attribute that I can use in the equip.properties?

DataGridView.Datasource = EquipList;

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

public class equip
{
 public int EquipID {get;set;}
 public string EquipName {get;set;}
}

Okay, maybe I didn't explain myself properly...I am hoping that that there is an attribute that could be applied like....

[Alis("name I would like to see in the gridview")]
public int EquipID {get;set;}

if not I'll just stick with the old tired ways

Later on that same morning....

I got it!

 using System.ComponentModel;

[DisplayName("the nice name")]
 public int EquipID {get;set;}

回答1:


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;}
  }



回答2:


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.



来源:https://stackoverflow.com/questions/5551418/how-do-i-set-columnnames-for-a-datagridview-bound-to-a-listt

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