winforms: aligning of column headers
-
Hi. Is there any way to center-align columnheaders in a datagrid while the text in the rows of the column is still rigth-aligned? I found out using datagridTextBox.Alignment, but this aligns both header AND text in the rows... any help?
-
Hi. Is there any way to center-align columnheaders in a datagrid while the text in the rows of the column is still rigth-aligned? I found out using datagridTextBox.Alignment, but this aligns both header AND text in the rows... any help?
You could create your own
DataGridColumnStyle
derivatives and ignore theAlignment
property. Just derive from the existing ones likeDataGridTextBoxColumn
andDataGridBoolColumn
to avoid having to implement the rest of the behavior.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
You could create your own
DataGridColumnStyle
derivatives and ignore theAlignment
property. Just derive from the existing ones likeDataGridTextBoxColumn
andDataGridBoolColumn
to avoid having to implement the rest of the behavior.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
but the columnstyke also aligns both the text and the headers, right? So unless i override the paint event, I can't do what i want....
-
but the columnstyke also aligns both the text and the headers, right? So unless i override the paint event, I can't do what i want....
In the override for
DataGridColumnStyle.Alignment
, try returningHorizontalAlignment.Left
. The default painting procedure should use that to paint the text; otherwise, yes, you will have to do the text painting yourself (but that is pretty easy to do). SeeGraphics.MeasureString
andGraphics.DrawString
for more information.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----