C# DataGridView 编辑列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/125719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView Edit Column Names
提问by Dested
Is there any way to edit column names in a DataGridView?
有没有办法在 DataGridView 中编辑列名?
采纳答案by Muxa
I don't think there is a way to do it without writing custom code. I'd implement a ColumnHeaderDoubleClick event handler, and create a TextBox control right on top of the column header.
我认为不编写自定义代码就没有办法做到这一点。我会实现一个 ColumnHeaderDoubleClick 事件处理程序,并在列标题的顶部创建一个 TextBox 控件。
回答by VVS
I guess what you want is to edit the HeaderText property of the column:
我想你想要的是编辑列的 HeaderText 属性:
myDataGrid.TableStyles[0].GridColumnStyles[0].HeaderText = "My Header"
Source: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=186908&SiteID=1
来源:http: //forums.microsoft.com/MSDN/ShowPost.aspx?PostID=186908&SiteID=1
回答by mattlant
You can also edit directly without knowing anything as posted above :
您也可以直接编辑而不知道上面发布的任何内容:
protected void gvCSMeasureCompare_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
e.Row.Cells[0].Text = "New Header for Column 1";
}
回答by Ryan Spears
You can also change the column name by using:
您还可以使用以下方法更改列名称:
myDataGrid.Columns[0].HeaderText = "My Header"
but the myDataGrid
will need to have been bound to a DataSource
.
但是myDataGrid
将需要绑定到DataSource
.
回答by Ryan Spears
@Dested if you are populating DataGrid from DataReader, you can change the name of columns in your query
@Dested 如果您从 DataReader 填充 DataGrid,则可以更改查询中的列名
for example
例如
select ID as "Customer ID", CstNm as "First Name", CstLstNm as "Last Name"
from Customers
this way in your data grid you will see Customer ID instead of ID and so forth.
这样在您的数据网格中,您将看到客户 ID 而不是 ID 等等。
回答by Bj?rn Otto Vasbotten
You can edit the header directly:
您可以直接编辑标题:
dataGridView1.Columns[0].HeaderCell.Value = "Created";
dataGridView1.Columns[1].HeaderCell.Value = "Name";
And so on for as many columns you have.
对于您拥有的尽可能多的列,依此类推。
回答by user3698169
Try this
尝试这个
myDataGrid.Columns[0].HeaderText = "My Header"
myDataGrid.Bind() ;