user710502
user710502

Reputation: 11471

How can I change the header of the DataGridViewColumn in Windows Forms?

I checked on other postings and suggested to make the EnableHeadersVisualStyles = false but that only lets me color them. I want to change their names, but they don't change when I force the name as shown below.

Products prod = new Products();
DataTable dt = prod.GetTopTenTransactions(Global.Instance.Accounts[0]);
transactionGrid.DataSource = dt;

foreach (DataGridViewColumn cols in transactionGrid.Columns)
{
    cols.Width = 70;
}

transactionGrid.ColumnHeadersDefaultCellStyle.BackColor = Color.CadetBlue;
transactionGrid.EnableHeadersVisualStyles = false; 

transactionGrid.Columns[0].Name = "ID";
transactionGrid.Columns[1].Name = "TYPE";
transactionGrid.Columns[2].Name = "DATE";
transactionGrid.Columns[3].Name = "AMOUNT";
transactionGrid.Columns[4].Name = "FROM";
transactionGrid.Columns[5].Name = "TO";
transactionGrid.Columns[6].Name = "TELLER ID";

Any help would be much appreciated.

Upvotes: 1

Views: 2572

Answers (3)

Ankur Sharma
Ankur Sharma

Reputation: 283

  1. If you are using a predefined set of columns (known at design time), then in visual studio select the datagrid, go to properties in columns click the ... button this opens up an extra window, in which you can edit the details of your column

  2. If you are doing things at runtime, then in the code behind file use:

    datagrid.Columns[index].HeaderText = "new value";

Hope this helps.

Upvotes: 0

ionden
ionden

Reputation: 12776

You should use HeaderText property:

transactionGrid.Columns[0].HeaderText = "Header";

Upvotes: 5

default locale
default locale

Reputation: 13426

DataGridViewColumn.Name is used to identify column in collection.
Try to use DataGridViewColumn.HeaderText instead.

Upvotes: 5

Related Questions