How to set column header text for specific column in Datagridview C#
This question is related to
c#
.net
winforms
datagridview
Dg_View.Columns.Add("userid","User Id");
Dg_View.Columns[0].Width = 100;
Dg_View.Columns.Add("username", "User name");
Dg_View.Columns[0].Width = 100;
For info, if you are binding to a class, you can do this in your type via DisplayNameAttribute
:
[DisplayName("Access key")]
public string AccessKey { get {...} set {...} }
Now the header-text on auto-generated columns will be "Access key".
dgv.Columns[0].HeaderText = "Your Header";
grid.Columns[0].HeaderText
or
grid.Columns["columnname"].HeaderText
private void datagrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string test = this.datagrid.Columns[e.ColumnIndex].HeaderText;
}
This code will get the HeaderText
value.
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";
Source: Stackoverflow.com