Is there any way to edit column names in a DataGridView?
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.
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
.
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.
@Dested if you are populating DataGrid from DataReader, you can change the name of columns in your query
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.
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
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";
}
Try this
myDataGrid.Columns[0].HeaderText = "My Header"
myDataGrid.Bind() ;
来源:https://stackoverflow.com/questions/125719/datagridview-edit-column-names