具有关联数据源的 DataGridView 的自定义列名 [英] Custom column names for DataGridView with associated DataSource

查看:56
本文介绍了具有关联数据源的 DataGridView 的自定义列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为带有关联数据源的 DataGridView 设置自定义列名?

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

这是一些代码:

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;

这为我提供了列名为Value"和ExpirationDate"的 dataGridView.例如,我应该如何将名称更改为密钥"和过期"?

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

推荐答案

使用 DisplayName 属性用于在 DataGridView 中指定列名:

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

这篇关于具有关联数据源的 DataGridView 的自定义列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆