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?
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;
There is a property HeaderCell
on the column object which allow you to access the style:
dataGridView1.Columns(0).HeaderCell.Style.Alignment
= DataGridViewContentAlignment.BottomCenter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With