Friday, February 24, 2006

DataGridView Column Header Style

I was building a .NET 2.0 Windows application which contained a DataGridView object.  I was attempting to modify the column header styles to fit better in the application but none of the settings I was applying seemed to be taking effect.  So finally I posted a message on Channel9 and got a response with the answer to my question.

In order to use styles in your DataGridView control for your .NET 2.0 Windows application, you must specify dgvMyDataGridView.EnableHeadersVisualStyles = false.

Example of the code:
//Instantiate new DataGridViewCellStyle
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 =
    new System.Windows.Forms.DataGridViewCellStyle();

//Define Header Style
dataGridViewCellStyle2.Alignment =
    System.Windows.Forms.DataGridViewContentAlignment.BottomCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif",
    8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle2.SelectionBackColor =
    System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.WrapMode =
    System.Windows.Forms.DataGridViewTriState.True;

//Apply Header Style
this.dgvTransactions.ColumnHeadersDefaultCellStyle =
    dataGridViewCellStyle2;

//Disable Headers Visual Styles - apparently there is an "automatic"
// visual style that needs to be disabled
this.dgvTransactions.EnableHeadersVisualStyles = false;

Tags: , , ,

Submit this story to DotNetKicks

0 comments: