Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem setting alignment on a datagrid 1

Status
Not open for further replies.

Elena

Technical User
Oct 20, 2000
112
US
Hi,

I am trying to set the alignment of the data in the grid control to right-aligned for a column that contains currency. That is working using the DataGridTextBoxColumn Class. However, it sets the entire column to right-aligned, including the Column header text, which doesn't look good.

Is there a way to set the header text alignment separately?

Thanks,
Elena
 
Shelton,

Thanks for the reply. However, I did not see how to set the alignment, only the text.

I still haven't figured this one out.

Thanks,
 
Okay, here is a quick attempt to try and help you. Note that I haven't tested this fully so I don't know the full impact of doing this.

First, you need to store the location of the top left visible cell in the datagrid. This can be done in the Form_Load event (but after the datagrid datasource has been set):
Code:
Private ptTop As Point

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ptTop = New Point(DataGrid1.GetCellBounds(0, 0).X + 4, DataGrid1.GetCellBounds(0, 0).Y + 4)

End Sub
Then handle the Paint event of the datagrid and paint over the existing header text with our newly aligned text:
Code:
Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint

        Dim ts As DataGridTableStyle = DataGrid1.TableStyles(0)

        For Each dgcs As DataGridColumnStyle In ts.GridColumnStyles

            If TypeOf dgcs Is DataGridTextBoxColumn AndAlso CType(dgcs, DataGridTextBoxColumn).Format = "c" Then

                Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(ptTop)

                Dim dgc As New DataGridCell(hti.Row, ts.GridColumnStyles.IndexOf(dgcs))

                Dim pt As Point = DataGrid1.GetCellBounds(dgc).Location

                Dim r As New Rectangle(pt.X, pt.Y - 17, dgcs.Width - 1, 16)

                'Paint over the existing text

                e.Graphics.FillRectangle(New SolidBrush(DataGrid1.HeaderBackColor), r)

                Dim sf As New StringFormat

                sf.Alignment = StringAlignment.Center

                sf.Trimming = StringTrimming.EllipsisWord

                sf.FormatFlags = StringFormatFlags.NoWrap

                'Draw the new text, with our alignment

                e.Graphics.DrawString(dgcs.HeaderText, DataGrid1.HeaderFont, New SolidBrush(DataGrid1.HeaderForeColor), RectangleF.op_Implicit(r), sf)

            End If

        Next

    End Sub
The above assumes that there is a single TableStyle applied to the grid, and that the header row is the standard height. It should get you on the right track, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top