Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;}
like image 389
Brad Avatar asked Apr 05 '11 11:04

Brad


1 Answers

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;}
  }
like image 184
Brad Avatar answered Nov 09 '22 18:11

Brad