How to set column header text for specific column in Datagridview C#
7 Answers
there is HeaderText property in Column object, you can find the column and set its HeaderText after initializing grid or do it in windows form designer via designer for DataGrid.
public Form1()
{
InitializeComponent();
grid.Columns[0].HeaderText = "First Column";
//..............
}
More details are here at MSDN. More details about DataGrid are here.
2 Comments
Nathan McKaskle
How do you accomplish the same thing without calling the cellvaluechanged event?
amantur
Well it has been some time since I used this, but I think if you do it via designer (if you know before hand what you want the column value to be) it should not bother to fire event. Or you can set some marker like
settingHeader=true; and then check for this in callvaluechanged handler to skip any action.If you work with visual studio designer, you will probably have defined fields for each columns in the YourForm.Designer.cs file e.g.:
private System.Windows.Forms.DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
If you give them useful names, you can set the HeaderText easily:
usefulNameForDataGridViewTextBoxColumn.HeaderText = "Useful Header Text";
Comments
dgv.Columns[0].HeaderText = "Your Header";
1 Comment
Nic3500
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.