DataGridView Edit Column Names

2020-02-10 04:39发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

@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.



回答5:

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



回答6:

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";
}


回答7:

Try this

myDataGrid.Columns[0].HeaderText = "My Header"
myDataGrid.Bind() ;