We know we can align the cell contents with the following two ways:
1. configure it in design mode
In design mode, select the datagridview, right click mouse and select Edit Columns. Select the column and configure the DefaultCellStyle.
2. by code
Code:DataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
But how to align the header text in datagridview? We have to write a little bit code, see the following examples:
Align the headings of all columns:
Code:dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
Align the heading of an individual column:
Code:deptDataGridViewTextBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
You need to set the sort mode to not sortable:
Code:DataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable
//Aligns the Header Text
dataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight