views:

609

answers:

4

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?

+1  A: 

For column headers you can do this

DataGrid d = new DataGrid();
d.Columns[0].HeaderStyle.VerticalAlign = VerticalAlign.Bottom;

For cell content you can do this

DataGrid d = new DataGrid();
d.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;

EDIT: Just realised that you were asking about GridViews - This will be the same for that too

eg
GridView gv = new GridView ();
gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;

Hope this helps

Matt Joslin
i want datagrid view not datagrid.datagridview not having this property. please help me sir
somu
sorry question was tagged with 'DataGrid' - also you might like to accept a few answers to encourage others to help you
Matt Joslin
thank you for your help
somu
A: 

datagridview.Columns(linti).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter

here linti means columnno or id

somu
+1  A: 

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;
David Hall
A: 

Change

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

to

DataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
Gabriel McAdams