How to align text for a single subitem in ListView using C #?

I could not find the answer anywhere in this seemingly simple topic: Is it possible to align the text of one subitem in a ListView WinForms control?

If so, how?

I would like the text in the same column to align differently.

+6
c # listview text-alignment
source share
4 answers

In the future, here, as I decided this:

// Make owner-drawn to be able to give different alignments to single subitems lvResult.OwnerDraw = true; ... // Handle DrawSubItem event private void lvResult_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { // This is the default text alignment TextFormatFlags flags = TextFormatFlags.Left; // Align text on the right for the subitems after row 11 in the // first column if (e.ColumnIndex == 0 && e.Item.Index > 11) { flags = TextFormatFlags.Right; } e.DrawText(flags); } // Handle DrawColumnHeader event private void lvResult_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { // Draw the column header normally e.DrawDefault = true; e.DrawBackground(); e.DrawText(); } 

It was necessary to process DrawColumnHeader, otherwise the text or column separators would not be drawn.

+5
source share

The "ColumnHeader" class has the "TextAlign" property, which will change the alignment for all subitems in the column. If you need something more unusual, you can always use the DrawSubItem event and make it an owner.

+9
source share

example:

 listView1.Columns[1].TextAlign = HorizontalAlignment.Right; 

sets column alignment "1" to the right

+7
source share

Note. Due to the limitation of the main ListView inline control (living in comctl32.dll), the first column cannot be aligned. It will always be aligned to the left. The second limitation is when you set up drawing (custom paint fakes): when you enable column reordering, the text of the first column is NOT correctly reordered. I solved this restriction (I would not call it an error, because listview supports many list styles, and the internal data structure in the form of a list is a tree similar to one), not allowing you to reorder the first column, which in most cases does not present a problem, because that you will use some kind of key for the first column, for example, a number or something like that.

+6
source share

All Articles