Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
- I got a problem when sort column in listview
- This is my code:
+In class ListViewItemComparer:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace ConvertPTU
{
    public class ListViewItemComparer : IComparer
    {
        private int column;
        private bool numeric = false;
        public int Column
        {
            get { return column; }
            set { column = value; }
        }
        public bool Numeric
        {
            get { return numeric; }
            set { numeric = value; }
        }
        public ListViewItemComparer(int columnIndex)
        {
            Column = columnIndex;
        }
        public int Compare(object x, object y)
        {
            ListViewItem listX = (ListViewItem)x;
            ListViewItem listY = (ListViewItem)y;
            if (Numeric)
            {
                // Chuyển text thành số trước khi so sánh.
                // Nếu chuyển đổi thất bại, sử dụng giá trị 0.
                decimal listXVal, listYVal;
                try
                {
                    listXVal = Decimal.Parse(listX.SubItems[Column].Text);
                }
                catch
                {
                    listXVal = 0;
                }
                try
                {
                    listYVal = Decimal.Parse(listY.SubItems[Column].Text);
                }
                catch
                {
                    listYVal = 0;
                }
                return Decimal.Compare(listXVal, listYVal);
            }
            else
            {
                // Giữ nguyên text ở định dạng chuỗi
                // và thực hiện so sánh theo thứ tự alphabetic.
                string listXText = listX.SubItems[Column].Text;
                string listYText = listY.SubItems[Column].Text;
                return String.Compare(listXText, listYText);
            }
        }
    }
}


events columnClick
C#
private void listView_ListSite_ColumnClick(object sender, ColumnClickEventArgs e)
{
    ListViewItemComparer sorter = new ListViewItemComparer(e.Column);
    listView_ListSite.ListViewItemSorter = sorter;
    listView_ListSite.Sort();
}


My code working with columns has data
but column hasn't data that it got error : "InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index
"

P/S : My listView has 4 column

please,help me!!!!
I really need your help !!!
thanks
regards
Posted

1 solution

Event though your listview has 4 columns, it doesn't automatically generate the subitems. For each item in the listview, you need to make sure that it has enough subitems, and if not, you need to add them (even if their text is empty).

I would guess that that is the source of the problem.
 
Share this answer
 
Comments
sergio090588 13-May-13 10:27am    
yes.I think this
but I don't know how I must edit my code?
you can help me ?
Johnny J. 13-May-13 10:30am    
What you have to when you add the listviewitems is something like this:

ListViewItem itm = new ListViewItem("TestText"); //Setting the text will automatically add SubItems[0]

itm.SubItems.Add(""); //SubItems[1]
itm.SubItems.Add(""); //SubItems[2]
itm.SubItems.Add(""); //SubItems[3]

ListView1.Items.Add(itm);
sergio090588 13-May-13 11:18am    
thanks for your help but not solve my issue yet
Johnny J. 13-May-13 11:38am    
I cant help you unless you post the code that adds the items to the ListView. Thats where the problem is.
sergio090588 13-May-13 12:27pm    
ok man
This is code add the items to the ListView
listView_ListSite.Items.Clear();
StreamReader sr = new StreamReader("E:\\test.txt");
while (!sr.EndOfStream)
{
string readline = sr.ReadLine();
string[] mang = readline.Split('|');
ListViewItem item;
if (mang.Length == 4)
{
item = new ListViewItem(mang[0].Trim().ToString());
item.SubItems.Add(mang[1].Trim().ToString());
item.SubItems.Add(mang[2].Trim().ToString());
item.SubItems.Add(mang[3].Trim().ToString());
listView_ListSite.Items.Add(item);
}
else if (mang.Length == 3)
{
item = new ListViewItem(mang[0].Trim().ToString());
item.SubItems.Add(mang[1].Trim().ToString());
item.SubItems.Add(mang[2].Trim().ToString());
listView_ListSite.Items.Add(item);
}
}
sr.Close();

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