1

I have a problem when try to change datagridview`s column header name.

After i changed the column header name from name Column 1 to Year i am trying to get the value of the cell of that column with command

  1. row.cells("HEADERNAME").value

and it says that Year column cannot be found. I tried the command to row.cells("Column1").value after is changed to Year and it works .

whys that ? is there anything else i have to do in order to save changes ? i can see that the column header name is changed normaly.

3
  • Show the code where you change the name of the column. Commented Aug 16, 2015 at 7:36
  • DataGridView1.Columns(i).HeaderCell.Value = "VALUE" Commented Aug 16, 2015 at 7:48
  • If you want to change the name of a column, use the name property: Me.DataGridView1.Columns(i).Name = "Year" Commented Aug 16, 2015 at 7:50

1 Answer 1

1

The text in the header, and the column name are two different entities. Changing one does not automatically change the other.

You should refer to the column by it's name, as you have discovered:

row.Cells("Column1").Value 

Alternatively, if you want to change the name of the column, you can do so. Assuming you have an underlying DataTable as a data source:

dataTable.Columns("Column1").Name = "MyNewName"

If it's not a data table, you should be able to change the name directly in the DataGridView:

dataGridView.Columns("Column1").Name = "MyNewName"
Sign up to request clarification or add additional context in comments.

2 Comments

so how i can change the header text and also the actual column name of a column ?
No i dont have a datatable because i just load a csv file to datagrid and then i want to get the value according to the changed column name to insert it to my sql table

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.