Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView header alignment

I am using vb.net 2005. I want one clarification for datagridview.

I use the following property to set the alignment of header text:

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

This property applies to all header cells. I only want to change one headers alignment property.

How can this be done?

like image 915
somu Avatar asked Jan 20 '10 12:01

somu


2 Answers

To change just the one header cell you need to select the column you want from the DataGridView columns collection.

dataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

dataGridView1.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

I've shown two examples - one using the column index in the columns collection, the other using the name given to the column.

Each column in the DataGridView then has a HeaderCell property, with a Style.Alignment that can be set with a value from the DataGridViewContentAlignment enum.

One additional thing to note is that these alignments are affected by the sorting glyph in the column. If the alignment does not appear how you expect, the code below can sometimes resolve the issue by removing the sorting glyph.

dataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable;
like image 197
David Hall Avatar answered Oct 16 '22 01:10

David Hall


There is a property HeaderCell on the column object which allow you to access the style:

dataGridView1.Columns(0).HeaderCell.Style.Alignment 
  = DataGridViewContentAlignment.BottomCenter
like image 39
Maxence Avatar answered Oct 16 '22 00:10

Maxence