views:

347

answers:

3

I have listView in my (C#) program. I change some items background and foreground colors after I have added items to listview. All items are OK and Colors too. If I use sorting with listView, then all first 6 items colors have been disappeared. And some colors of rest items are in disorder. This effect happens with default sorting and my own custom sorter. I do many changes to listView items data and finally when listView has been sorted many times all colors are in right positions. What is wrong?

A: 

No, colors are not staying with same rows.

Jari Koskinen
A: 

Messed around for a little and I'm not sure what you could be doing wrong. Seems to work fine for me but without seeing your code it's hard to say what is happening. In any case here's a simple working example that might help you figure out what is different. Just create a form with a 3 column ListView in Details view:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Color[] backgroundColors = new[] { Color.Green, Color.White, Color.Blue };
        Color[] foregroundColors = new[] { Color.Black, Color.Red, Color.Yellow };

        Random random = new Random();

        for (int i = 0; i < 100; i++)
        {
            Color backgroundColor = backgroundColors[random.Next(0, 3)];
            Color foregroundColor = foregroundColors[random.Next(0, 3)];

            ListViewItem listViewItem = listView1.Items.Add(foregroundColor.Name);
            listViewItem.SubItems.Add(backgroundColor.Name);
            listViewItem.SubItems.Add(Guid.NewGuid().ToString());
            listViewItem.BackColor = backgroundColor;
            listViewItem.ForeColor = foregroundColor;
        }

        listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    }

    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        ListViewItemComparer listViewItemComparer = new ListViewItemComparer {SortColumn = e.Column, Sorting = SortOrder.Ascending};

        if (listView1.ListViewItemSorter is ListViewItemComparer)
        {
            ListViewItemComparer listViewItemComparerOld = listView1.ListViewItemSorter as ListViewItemComparer;

            if (listViewItemComparerOld != null && listViewItemComparerOld.SortColumn == e.Column)
            {
                listViewItemComparer.Sorting = (listViewItemComparerOld.Sorting == SortOrder.Ascending) ? SortOrder.Descending : SortOrder.Ascending;
            }
        }

        listView1.ListViewItemSorter = listViewItemComparer;
        listView1.Sort();
    }

    #region ListViewItemComparer
    public class ListViewItemComparer : IComparer
    {
        #region Public Properties
        public int SortColumn { get; set; }
        public SortOrder Sorting { get; set; }
        #endregion

        public ListViewItemComparer()
        {
            SortColumn = 0;
            Sorting = SortOrder.Ascending;
        }

        public int Compare(object x, object y)
        {
            ListViewItem listViewItem1 = null;
            ListViewItem listViewItem2 = null;

            string compare1 = string.Empty;
            string compare2 = string.Empty;

            if (Sorting == SortOrder.Ascending)
            {
                listViewItem1 = (ListViewItem)x; listViewItem2 = (ListViewItem)y;
            }
            if (Sorting == SortOrder.Descending)
            {
                listViewItem1 = (ListViewItem)y; listViewItem2 = (ListViewItem)x;
            }

            if (listViewItem1 != null && (SortColumn < listViewItem1.SubItems.Count) && (listViewItem1.SubItems[SortColumn] != null))
            {
                compare1 = listViewItem1.SubItems[SortColumn].Text;
            }

            if (listViewItem2 != null && (SortColumn < listViewItem1.SubItems.Count) && (listViewItem2.SubItems[SortColumn] != null))
            {
                compare2 = listViewItem2.SubItems[SortColumn].Text;
            }

            return string.Compare(compare1, compare2);
        }
    }

    #endregion

}
Cory Charlton
Sorry, my program change some of subitems colors.
Jari Koskinen
A: 

Now it works. I just turn off sorting:

listView1.ListViewItemSorter = null;

And after all changes has been made, sorting is turned on again:

listView1.ListViewItemSorter = new Sorter();

Jari Koskinen