25
votes

I'm trying to change the text alignment and the font size of a DataGridView. All the Columns are created programatically at runtime. Here's the code..

private void LoadData()
{
    dgvBreakDowns.ColumnCount = 5;
    dgvBreakDowns.Columns[0].Name = "Breakdown No";
    dgvBreakDowns.Columns[1].Name = "Breakdown Type";
    dgvBreakDowns.Columns[2].Name = "Machine Type";
    dgvBreakDowns.Columns[3].Name = "Date";
    dgvBreakDowns.Columns[4].Name = "Completed";

    dgvBreakDowns.Columns[4].Visible = false;

    foreach (DataGridViewHeaderCell header in dgvBreakDowns.Rows)
    {
        header.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        header.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
    }
}

This LoadData() method is called in the Form's constructor. The Columns are created but their Headers' changes don't apply. I think its because of a flaw in my loop foreach (DataGridViewHeaderCell header in dgvBreakDowns.Rows)? I'm not sure. I tried changing it to dgvBreakDowns.Columns and I get an InvalidCastException. How can I select the Header Cells to apply those changes?

I have another minor issue. When I run the program it looks like this.

enter image description here

Notice the first Cell is selected by default therefore it appears Blue. Sure it doesn't affect anything but it just looks somewhat ugly and untidy. It is possible to stop it from selecting the Cell like that?

4
That's odd that you can unbox a DataGridViewRow to a DataGridViewHeaderCell like that. I think the only reason your code works is the Rows collection is empty...lc.

4 Answers

46
votes

Try this (note I'm using Columns here and not Rows):

foreach(DataGridViewColumn col in dgvBreakDowns.Columns)
{
    col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}

As for deselecting the cell, try dgvBreakDowns.ClearSelection()

6
votes

or just try this:

dgvBreakDowns.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;
2
votes

You can use the ColumnHeadersDefaultCellStyle.Alignment with MiddleCenter as its value. Check the code below:

dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
1
votes

Just for reference; there are some things to take into consideration in order for the control to show the customized Styles correctly. In this situation regarding Column Headers; but these do apply to further customization:

  • Set: "dataGridView.EnableHeadersVisualStyles" to false.
  • Apply styles after the DatagridView is Visible.
  • To align Header Columns; the "AutoSizeColumnMode" must be Set to Fill.

To ensure that all the customized Styles are applied after the Control is Visible; you can use the "DataGridView_VisibleChanged" Event.


DataGridView Visible Changed Event

/// <summary> Occurrs whenever the Control gets visible. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView_VisibleChanged(object sender, EventArgs e)
{
    if (dataGridView.Visible)
    {
        DataGridView_AutoSizeColumn();
        DataGridView_CentreHeaders();
        // Etc...
    }
}

Header Columns AutoSizeMode

private void DataGridView_AutoSize()
{
   var col = dataGridView.Columns;

   for (int i = 0; i < col.Count; i++)
   {
      if (i == 0) { col[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; }
      if (i == 1) { col[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; }
      // Etc...
    }
}

Centre Header Columns Text and Set Font

/// <summary> Centre Columns Headers. </summary>
private void DataGridView_CentreHeaders()
{
    // Centre Column Cells Content
    dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

    // Centre (Column and Row) Headers    
    dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

    // Set Font
    dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 11F, FontStyle.Regular, GraphicsUnit.Pixel);
}