Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello there!

Please help me I don;t know how to align the contents of a column in my ListView.. I can align the ColumnHeader but I dont know how to align its contents..

Please Help
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-14 0:48am    
You don't need to do anything for alignment. If you see anything out of alignment, I wonder how could you do it. Some code sample could greatly help.
Please see: SSCCE.
—SA
Member 9529406 9-Mar-14 1:57am    
I dont use any codes for creating columns in my listview sir I just create it using the design
Sergey Alexandrovich Kryukov 9-Mar-14 12:03pm    
Code created by the designer is still code.
—SA

1 solution

Column Alignment of Header, Body and Footer
In order to set the same alignment for all columns, we need to use ColumnFormatStyle property. This style allows as aligning of all columns at the same time and independently for column header, body and footer. For example:
VB
' Aligns the content of column header to center
Me.listView1.ColumnFormatStyle.HeaderTextAlign = ContentAlignment.MiddleCenter

' Aligns the content of column body to center
Me.listView1.ColumnFormatStyle.ContentAlign = HorizontalAlignment.Center

' Aligns the content of column footer to right
Me.listView1.ColumnFormatStyle.FooterTextAlign = ContentAlignment.MiddleRight


However, if we want to align a specific column separately from other columns, at first we must set the StyleFromParent property to false (to override the default settings). After that we can use the FormatStyle property which has the same fields for alignment of header, body and footer separately. In this case the code will be:
VB
' Override default settings
column.StyleFromParent = false;

' Aligns the content of this column header to center
column.FormatStyle.HeaderTextAlign = ContentAlignment.MiddleCenter

' Aligns the content of this column body to center
column.FormatStyle.ContentAlign = HorizontalAlignment.Center

' Aligns the content of this column footer to right
column.FormatStyle.FooterTextAlign = ContentAlignment.MiddleRight


Horizontal Alignment of SubItems
All subitems by default inherit the alignment of its content from their parent column settings. In order to set alignment for each subitem separately from other subitems, just like for columns, we at first need to set StyleFromParent property to false, and then change the default alignment:
 Aligns the text of this subitem to center
subItem.FormatStyle.TextAlign = ContentAlignment.MiddleCenter


Ripped from here[^]

-KR
 
Share this answer
 
Comments
Member 9529406 9-Mar-14 3:17am    
there was an error sir it says that... columnformatstyle is not a member of system.windows.forms.listview .....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900