【发布时间】:2010-03-21 09:10:18
【问题描述】:
如何更改DatagridView 中的标题文本以及如何在 C# 代码中添加或删除列?
【问题讨论】:
-
添加[WinForms]标签真的很麻烦吗?
如何更改DatagridView 中的标题文本以及如何在 C# 代码中添加或删除列?
【问题讨论】:
如果您使用数据绑定到一个类型和自动生成的列,这是[DisplayName(...)],即
[DisplayName("Last name")]
public string LastName {get;set;}
否则这是列上的HeaderText,即
grid.Columns[0].HeaderText = "Something special";
添加列的基本方法是:
int columnIndex = grid.Columns.Add("columnName", "Header Text");
或者您可以更具体,例如添加一列超链接:
grid.Columns.Add(new DataGridViewLinkColumn());
(您显然可以先在新列上设置更多属性)
【讨论】:
dataGridView1.Columns.Add("colName", "colHeaderText");
这是添加列并设置其标题文本的最简单方法,但如果您希望列有用,遵循@Marc Gravell 的建议可能会更有用。
【讨论】:
试试这个对我有用...
dataGridView1.Columns[datagridview1.CurrentCell.ColumnIndex].HeaderText = "newHeaderText";
【讨论】: