【问题标题】:How can I set the column name in a datagridview to something more user friendly than the columns of the table?如何将 datagridview 中的列名设置为比表的列更用户友好的名称?
【发布时间】:2019-10-09 09:06:33
【问题描述】:

下面是我目前正在使用的代码 - 尝试使用 Caption 作为在 datagridview 中显示的名称:

DataTable dt = new DataTable();
DataColumn column = new DataColumn
{
    DataType = System.Type.GetType("System.Int32"),
    AllowDBNull = false,
    Caption = "Start Day",
    ColumnName = "startDay",
    DefaultValue = 0
};
dt.Columns.Add(column);

【问题讨论】:

标签: c# datagridview caption


【解决方案1】:

您可以通过这种方式更改表中列的名称,并通过更新 DataGrid 的 DataSource 来更改名称。

DataTable dt = new DataTable(); DataColumn column = new DataColumn { DataType = System.Type.GetType("System.Int32"), AllowDBNull = false, Caption = "Start Day", ColumnName = "startDay", DefaultValue = 0 }; dt.Columns.Add(column); dt.Columns[0].ColumnName = "MyNewColumnName"; dt.Columns["MyNewColumnName"].ColumnName = "OtherName";

【讨论】:

    【解决方案2】:
    dgv.Columns[0].HeaderText = "Title Goes Here."; 
    

    对每一列重复并更改索引

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2015-04-16
      • 1970-01-01
      • 2011-02-28
      • 2020-03-14
      • 1970-01-01
      • 2010-11-27
      • 2012-05-10
      • 1970-01-01
      相关资源
      最近更新 更多