Changing alignment of column in datagrid

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

I thought I had done this before, but I'm apparently overlooking something.
All I want to do is set the alignment of the first column to the right. The
dataset gives me 5 columns and will auto-format the grid, but I really want
to align just one column. Thus the code below. However, the app blows right
past my attempt to impose a new table style and auto-formats the grid with
everything aligned to the left (I have deleted all table styles from the
Datagrid properties, so there is no "default").

Is it possible to set alignment for one column or do ALL need to be manually
aligned? Anything else I'm overlooking?

DataGrid1.TableStyles.Clear()
Dim tblStyle As New DataGridTableStyle
tblStyle.MappingName = "History"
Dim dgColumn1 As New DataGridTextBoxColumn
Dim dgColStyleRight As DataGridColumnStyle
dgColumn1 = New DataGridTextBoxColumn
dgColumn1.Alignment = System.Windows.Forms.HorizontalAlignment.Center
tblStyle.GridColumnStyles.Add(dgColumn1)
DataGrid1.CaptionText = "History"
DataGrid1.ReadOnly = True
DataGrid1.TableStyles.Add(tblStyle)
 
You can create a TableStyle, and then add it to the datagrid's tableStyles
collection. This will allow you to directly access the default
GridColumnStyle objects for each of the default columns. There, you can
explicitly set the properties on teh default columns without having to
explecitly add GridColumnStyles for every column.

Me.dataGrid1.DataSource = dt ' some DataTable...

Dim tblStyle As New DataGridTableStyle()
tblStyle.MappingName = dt.TableName

Me.dataGrid1.TableStyles.Clear()
Me.dataGrid1.TableStyles.Add(tblStyle)

Dim tbc As DataGridTextBoxColumn =
CType(tblStyle.GridColumnStyles("Col1"), DataGridTextBoxColumn)
tbc.Alignment = HorizontalAlignment.Center

======================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
Thanks Clay.

ClayB said:
You can create a TableStyle, and then add it to the datagrid's tableStyles
collection. This will allow you to directly access the default
GridColumnStyle objects for each of the default columns. There, you can
explicitly set the properties on teh default columns without having to
explecitly add GridColumnStyles for every column.

Me.dataGrid1.DataSource = dt ' some DataTable...

Dim tblStyle As New DataGridTableStyle()
tblStyle.MappingName = dt.TableName

Me.dataGrid1.TableStyles.Clear()
Me.dataGrid1.TableStyles.Add(tblStyle)

Dim tbc As DataGridTextBoxColumn =
CType(tblStyle.GridColumnStyles("Col1"), DataGridTextBoxColumn)
tbc.Alignment = HorizontalAlignment.Center

======================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
Back
Top