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

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

问题描述

如何使用关联的DataSource为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。我应该如何继续更改Key和Expire的名称?

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

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

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