First, set the alignment
1. Center-Aligned column headings
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = Datagridviewcontentalignment.middlecenter;
But in fact it still seems to be left, because there is a sort of triangle, you need to cancel the sorting function
2. Align Content Center
DataGridView1.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
Second, set the width of the column
Select DataGridView, set the autoSizeColumnsMode option, and its property value:
1.AllCells adjusts the column width to fit the contents of all the cells in the column, including the header cell.
2.AllCellsExceptHeader adjusts the column width to fit the contents of all cells in the column, excluding header cells.
3.ColumnHeader adjusts the column width to fit the contents of the column header cell.
4.DisplayedCells adjusts the column width to fit the contents of all the cells in the column of the row displayed on the current screen, including the header cell.
5.DisplayedCellsExceptHeader adjusts the column width to fit the contents of all cells in the column of the row displayed on the current screen, excluding header cells.
6.Fill Adjust the width of the column so that the widths of all columns fill the display area of the control exactly
7.None Column Width not adjusted
In the development process, the number of columns is not fixed, the final selection of fill
Iii. Example (AutoFit column width + cancel sort)
for (int i = 0; i < datagv.columncount;i++)
{
Datagv.columns[i]. AutoSizeMode = Datagridviewautosizecolumnmode.allcells;
Datagv.columns[i]. SortMode = datagridviewcolumnsortmode.notsortable;
}