DataGridView Edit Column Names

2020-02-10 04:59发布

Is there any way to edit column names in a DataGridView?

7条回答
SAY GOODBYE
2楼-- · 2020-02-10 05:02

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.

查看更多
Luminary・发光体
3楼-- · 2020-02-10 05:05

I guess what you want is to edit the HeaderText property of the column:

myDataGrid.TableStyles[0].GridColumnStyles[0].HeaderText = "My Header"

Source: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=186908&SiteID=1

查看更多
不美不萌又怎样
4楼-- · 2020-02-10 05:10

Try this

myDataGrid.Columns[0].HeaderText = "My Header"
myDataGrid.Bind() ;
查看更多
我命由我不由天
5楼-- · 2020-02-10 05:16

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.

查看更多
女痞
6楼-- · 2020-02-10 05:17

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.

查看更多
啃猪蹄的小仙女
7楼-- · 2020-02-10 05:17

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";
}
查看更多
登录 后发表回答