12

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

1
  • 1
    Is it really too much trouble to add a [WinForms] tag? Commented Mar 21, 2010 at 9:28

3 Answers 3

35

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using .Net 4.6.1 and my Datagrid.Columns[0] doesn't have HeaderText property.
1
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.

Comments

1

try this it worked for me...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.