We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to centre align data grid's header only

How to change the alignment of datagrids header to centre without disturbing the alignment of data in the rows. if i use tablestyles and set alignment of columns its affecting alignment of all rows and header. i want to centre align the header and the data may be aligned left/right depending on the data type.

1 Reply

JE Jeff May 23, 2004 01:54 PM UTC

Ok, My problem is that I am only able to align headers. Here is my procedure. Private Sub AddCellFormattingColumnStyles2(ByVal grid As DataGrid, ByVal handler As FormatCellEventHandler) Dim ts As DataGridTableStyle ts = New DataGridTableStyle Dim dt As DataTable dt = CType(grid.DataSource, DataTable) ts.MappingName = dt.TableName Dim j As Integer j = 0 Do While (j < dt.Columns.Count) Dim cs As DataGridFormattableTextBoxColumn cs = New DataGridFormattableTextBoxColumn(j) cs.Alignment = HorizontalAlignment.Right cs.MappingName = dt.Columns(j).ColumnName cs.HeaderText = dt.Columns(j).ColumnName ''cs.Alignment = HorizontalAlignment.Right ''cs.Width = -1 AddHandler cs.SetCellFormat, handler ts.GridColumnStyles.Add(cs) j = (j + 1) Loop grid.TableStyles.Clear() grid.TableStyles.Add(ts) Dim z As Int32 z = 0 For z = 0 To dt.Columns.Count - 1 grid.TableStyles(0).GridColumnStyles(z).ReadOnly = True Select Case z Case 0 grid.TableStyles(0).GridColumnStyles(z).Width = 250 ''grid.TableStyles(0).GridColumnStyles(z).Alignment = HorizontalAlignment.Right Case Else grid.TableStyles(0).GridColumnStyles(z).Width = 150 ''grid.TableStyles(0).GridColumnStyles(z).Alignment = HorizontalAlignment.Right End Select Next grid.TableStyles(0).AllowSorting = False grid.TableStyles(0).SelectionBackColor = System.Drawing.Color.Gold End Sub Hope this helps if not message me back. >How to change the alignment of datagrids header to centre without disturbing the alignment of data in the rows. if i use tablestyles and set alignment of columns its affecting alignment of all rows and header. i want to centre align the header and the data may be aligned left/right depending on the data type.

Loader.
Live Chat Icon For mobile
Up arrow icon