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?
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; } }
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With