Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom column names for DataGridView with associated DataSource

How can I setup custom column names for DataGridView with associated DataSource?

Here is some code:

class Key {     public string Value { get; }     public DateTime ExpirationDate { get; } }  List<Key> keys = new List<Key>(); ...// fill keys collection  DataGridView dataGridView = createAndInitializeDataGridView(); dataGridView.DataSource = keys; 

This gives me dataGridView with column names "Value" and "ExpirationDate". How should I proceed to change names to "Key" and "Expire" for example?

like image 879
Bobrovsky Avatar asked Jun 03 '11 14:06

Bobrovsky


2 Answers

Use the DisplayName attribute on your properties to specify column names in your DataGridView:

class Key {     [System.ComponentModel.DisplayName("Key")]     public string Value { get; }     [System.ComponentModel.DisplayName("Expire")]     public DateTime ExpirationDate { get; } }  
like image 110
Jay Riggs Avatar answered Sep 23 '22 04:09

Jay Riggs


You should be able to change the header cells after you've set the datasource:

    if (dataGridView1.Columns["Value"] != null)         dataGridView1.Columns["Value"].HeaderText = "Key";     if (dataGridView1.Columns["Expiration"] != null)         dataGridView1.Columns["Expiration"].HeaderText = "Expire"; 
like image 34
TBohnen.jnr Avatar answered Sep 24 '22 04:09

TBohnen.jnr