C# Datagridview align header in center

Asked

Viewed 2,787 times

1

I am suffering to be able to align the header of Datagridview. I want the text to be centered in the center.

Here’s 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:

inserir a descrição da imagem aqui

It is aligned in the center, but pulled to the left. Is there a way to put the text 100% centered? I’ve searched the forum but found no one with that problem.

The code is part of a function that receives a Datagridview, formats and returns with the formatting. Follow the complete code:

    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.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 the others by the code:

public void DataGridViewDesign(){
    Design modelo = new Design();
    dgDados = modelo.Grade(dgDados);
}

All application Grids look the same.

  • It is Windows Forms?

  • Yes, it’s Windows Forms. I used Visual Studio 2013.

  • @Lindomarlemos Place the section of routine responsible for calling the method Grade() also.

  • I put the code that calls the function. It is the same throughout the application.

  • I’m testing here.

  • @Lindomarlemos Adds the line in the method Grade() see if it works: dg.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

  • This code is for cell, in it worked leaving the text of the cells aligned to the left and the height centralized. The distance above and below is the same.

  • @Lindomarlemos I made a modification here and solved, left the Header in the center, I will edit my answer.

  • Sorry, I put the code in the wrong function. It centralized the cells perfectly the left and right side has the same distance. I just wish the same thing happened to the header.

Show 4 more comments

1 answer

2


You could specify the columns, see a small example:

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

Editing:

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:
https://social.msdn.microsoft.com/Forums/en-US/72b7fbac-660a-4693-9b7f-33ae6657853b/datagridview-headertext-alignment-padding-on-the-right-or-center?forum=csharplanguage

  • I also tried to apply in each column but the result was the same. Already in the cells works perfect. I find it very strange or missing I declare something in the code.

  • @Beautiful Try to leave the option EnableHeadersVisualStyles enabled (true).

  • Leaving the formatting enabled does not work. It is default with black font color, gray background and aligned to the left. I did other tests with right alignment and also gets a space until the edge playing the text more to the left.

  • @Lindomarlemos Post every routine responsible for formatting the question.

  • I put the full code of the function in the description, but outside the function happens the same thing.

  • @Let’s see if it worked now.

  • Very cool, it worked. Just can not put in the function. I don’t have an exact size of columns in each Datagrid where the function is called, I will see if I do another function by passing the number of columns and return this formatting. Thank you very much!

  • 1

    I did the function to align the Header. It must be called: dgDados = modelo.AlinharHeader(dgDados, dgDados.ColumnCount); Function: public DataGridView AlinharHeader(DataGridView dg, int colunas){&#xA; try{&#xA; for (int i = 0; i < colunas; i++){&#xA; dg.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;&#xA; dg.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;&#xA; }&#xA; }&#xA; catch { } &#xA; return dg;&#xA; }&#xA;

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.