Gold
Gold

Reputation: 62424

How to change header text in DatagridView - in code C#?

How to change header text in DatagridView and how to add or remove column - in C# code?

Upvotes: 12

Views: 58504

Answers (3)

sanjeev
sanjeev

Reputation: 600

try this it worked for me...

dataGridView1.Columns[datagridview1.CurrentCell.ColumnIndex].HeaderText = "newHeaderText";

Upvotes: 1

MrEdmundo
MrEdmundo

Reputation: 5165

dataGridView1.Columns.Add("colName", "colHeaderText");

This is the simplest method for adding a column and setting it's header text, although it might be much more useful to follow @Marc Gravell's advice if you want the column to be useful.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062540

If you are using data-binding to a type and auto-generated columns, this is the [DisplayName(...)], i.e.

[DisplayName("Last name")]
public string LastName {get;set;}

Otherwise this is the HeaderText on the column, i.e.

grid.Columns[0].HeaderText = "Something special";

A basic way to add a column is:

int columnIndex = grid.Columns.Add("columnName", "Header Text");

Or you can be more specific, for example to add a column of hyperlinks:

grid.Columns.Add(new DataGridViewLinkColumn());

(you could obviously set more properties on the new column first)

Upvotes: 35

Related Questions