473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Different header and text alignment in form datagrid

Is it possible to align headers and text in different way.
Because with:

dgts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center

alignment is set for all column (header and text).

Thanks for help!

Simon

Nov 21 '05 #1
3 4159
Hi,

Here is a columnstyle that I wrote that allows you to align the data
and column header differently. Set the alignment for the header and
dataalignment for the data. Hope this helps.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r), _

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class

Ken

---------------------------
"Simon Abolnar" <Si***********@tscng.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Is it possible to align headers and text in different way.
Because with:

dgts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center

alignment is set for all column (header and text).

Thanks for help!

Simon


Nov 21 '05 #2
Thank you very much!
I applied class, but I don't know how to use it.
How to apply class to datagrid?

Simon

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
Hi,

Here is a columnstyle that I wrote that allows you to align the
data
and column header differently. Set the alignment for the header and
dataalignment for the data. Hope this helps.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r),
_

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class

Ken

---------------------------
"Simon Abolnar" <Si***********@tscng.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Is it possible to align headers and text in different way.
Because with:

dgts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center

alignment is set for all column (header and text).

Thanks for help!

Simon

Nov 21 '05 #3
I have already solved the problem!

Thank you very much once again!

Simon

"Simon Abolnar" <Si***********@tscng.net> wrote in message
news:uY*************@TK2MSFTNGP12.phx.gbl...
Thank you very much!
I applied class, but I don't know how to use it.
How to apply class to datagrid?

Simon

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
Hi,

Here is a columnstyle that I wrote that allows you to align the
data
and column header differently. Set the alignment for the header and
dataalignment for the data. Hope this helps.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class

Ken

---------------------------
"Simon Abolnar" <Si***********@tscng.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Is it possible to align headers and text in different way.
Because with:

dgts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center

alignment is set for all column (header and text).

Thanks for help!

Simon


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the...
0
by: Frnak McKenney | last post by:
One of the reasons given for the Allied victory in WWI is that the Nazi armament industry invested so much effort in creating new weapons (e.g. the jet plane) it wasn't able to develop any of them...
0
by: Sam | last post by:
Hi guys, I tried to use DataGridTextBoxColumn alignment property to format my data in the datagrid and I don't quite get what I'd like it to be. The problem is that when I set the alignment...
2
by: John Smith | last post by:
Hi all; Putting "Due" into the column header of a datagrid. Font is a proportional fort. When the alignment is left, there is some space between the column separator bar and the D in Due....
1
by: robear | last post by:
Hi. I have a datagrid, it's datasource is a datset and has a tablesytle for formatting. What I need to do is to have the header either left or centre justifed and some of the columns need to...
4
by: Mortar | last post by:
i need a datagrid with 2 header columns. The top one might have 1 column spanning 5 columns of the header row below it. what is the best way to do this? Could i have 2 datatables...1 filling the...
1
by: Mrozu | last post by:
Hi In my DataGrid I have one column with prices and i need to align this column to left. But when i set algnment = left, header text is alignment to left too. And it can be, but last letters of...
1
by: Jose | last post by:
Please como center a datagrid header and the data justify to right? Thanks a lot
6
by: rn5a | last post by:
When the EditCommandColumn in a DataGrid is clicked, all the BoundColumns get replaced by TextBoxes so that users can alter the data. By default, the Text in the TextBoxes are left-aligned. Is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.