C # DataGridView align header in center

1

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.

    
asked by anonymous 05.06.2016 / 16:55

1 answer

2

You could specify the columns, see a small example:

dg.Columns["nomeDoMeuCampo"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

Issue:

I was able to leave the Header content in the center by disabling the sort property:

public DataGridView Grade(DataGridView dg)
{
    dg.EditMode = DataGridViewEditMode.EditProgrammatically;
    dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dg.AllowUserToAddRows = false;
    dg.AllowUserToDeleteRows = false;
    dg.DefaultCellStyle.Font = new Font("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.TopCenter;

    //Vc pode usar um for se quiser
    dg.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dg.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
    dg.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dg.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
    dg.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dg.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
    dg.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dg.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
    dg.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    dg.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;

    dg.RowsDefaultCellStyle.BackColor = Color.LightCyan;
    dg.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
    dg.MultiSelect = false;                        
    return dg;
}

Source:
link

    
05.06.2016 / 18:32