I'm suffering to get the DataGridView header aligned. I want the text to be centered in the center.
Here is the code:
dg.EnableHeadersVisualStyles = false; // Desabilita formatação padrão
dg.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
dg.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
dg.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
The result is as in the following figure:
Itisalignedinthecenter,butpulledtotheleft.Thereishowper100%centralizedtext?Ialreadysearchedtheforumbutfoundnoonewiththisproblem.
ThecodeispartofafunctionthatreceivesaDataGridView,formatsitandreturnsitwithformatting.Hereisthecompletecode:
publicDataGridViewGrade(DataGridViewdg){dg.EditMode=DataGridViewEditMode.EditProgrammatically;dg.SelectionMode=DataGridViewSelectionMode.FullRowSelect;dg.AllowUserToAddRows=false;dg.AllowUserToDeleteRows=false;dg.DefaultCellStyle.Font=newFont("Calibri", 9);
dg.EnableHeadersVisualStyles = false; // Desabilita formatação padrão
dg.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
dg.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
dg.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dg.RowsDefaultCellStyle.BackColor = Color.LightCyan;
dg.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
dg.MultiSelect = false;
return dg;
}
This function is in the Design class and is called in others by the code:
public void DataGridViewDesign(){
Design modelo = new Design();
dgDados = modelo.Grade(dgDados);
}
All application Grids look the same.