2

How to set a ListView to align some columns right and some columns left?

I mean that I have a ListView that its layout is left to right but I want to be alignment of second column and anything under that to left?

Thanks in advance.

#BEGIN EDIT

View property of the ListView is Details .

I'm using some code like this code:

        ListViewItem a = new ListViewItem();
        a.Text = "Some Name";
        a.SubItems.Add("Some Phone num");
        listView1.Items.Add(a);

And I want to be TextAlignment of the Phone Number right but I don't want to change alignment of Name.

#END EDIT

Mostafa Farzán
  • 899
  • 2
  • 11
  • 30
  • 1
    What type are the items you are trying to align? For instance, is the second sub item part of the first object? If for instance you're putting the second item differently because it's a second field, you might be best off using a template. Please post more details. – Mark Smith Jan 21 '12 at 13:17

1 Answers1

2

Something like this has been answered in past, maybe this will help you:

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

Community
  • 1
  • 1
Val Akkapeddi
  • 1,173
  • 10
  • 17
  • I could not change **alignment** of a **column**. So I enabled **OwnerDraw** and I solved that like him and then changed the **alignment** but when I insert a new item to the ListView, alignment of it is still the previous alignment! What is the problem? – Mostafa Farzán Jan 21 '12 at 15:54
  • In the code from the example I linked, the TextFormatFlags are only modified for column one after row 0. You should be doing something like the below. `lv.OwnerDraw = true;` `lv.DrawSubItem += new(lv_DrawSubItem);` `void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)` `{` ` var flags = TextFormatFlags.Left;` ` if (e.ColumnIndex == 1) { flags = TextFormatFlags.Right; }` ` e.DrawText(flags);` `}` – Val Akkapeddi Jan 23 '12 at 01:22