|
hai, how we can sort list view with regards Elby Paul - Moved byTaylorMichaelLMVPFriday, September 04, 2009 1:42 PMWinforms related (From:.NET Base Class Library)
-
| | Elby Paul Friday, September 04, 2009 12:43 PM |
Hi,
May be this link will help,
Regards,
Vinil;
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | VinilV Friday, September 04, 2009 1:50 PM | Hi,
You can set sorting property of listview:
listView1.Sorting = SortOrder.Descending;
ListView.Sorting Property
Gets or sets the sort order for items in the control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.sorting.aspx
Sorting ListView Items by Column Using Windows Forms
http://msdn.microsoft.com/en-us/library/ms996467.aspx#sorting_ascendingdescending
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | Ling Wang Friday, September 11, 2009 11:57 AM | You'll need some sorting classes, basically classes that implement the IComparer interface. I start with an abstract class:
public abstract class MyListViewSorter: IComparer
{
private int m_ColumnIndex = 0;
#region Properties
public int ColumnIndex
{
get {return this.m_ColumnIndex;}
set {this.m_ColumnIndex = value;}
}
#endregion
#region Methods
public virtual int Compare(object x, object y)
{
return 0;
}
#endregion
}
I then have a sorter class for all the types of columns I might have in my ListView. They all simply override the Compare method in the base class. Here's the basic 3: string, number, date:
public class StringListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
string s1, s2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
return string.Compare(s1, s2);
}
#endregion
}
public class NumberListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
Decimal i1, i2;
string s1, s2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
if (s1.Trim().Length == 0)
{
i1 = 0;
}
else
{
try
{i1 = Decimal.Parse(s1);}
catch (Exception)
{i1 = 0;}
}
if (s2.Trim().Length == 0)
{
i2 = 0;
}
else
{
try
{i2 = Decimal.Parse(s2);}
catch (Exception)
{i2 = 0;}
}
if (i1 < i2) {return -1;}
else if (i1 > i2) {return 1;}
else {return 0;}
}
#endregion
}
public class DateTimeListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
string s1, s2;
DateTime d1, d2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
if (s1.Trim().Length > 0)
d1 = DateTime.Parse(s1);
else
d1 = new DateTime(1900, 1, 1);
if (s2.Trim().Length > 0)
d2 = DateTime.Parse(s2);
else
d2 = new DateTime(1900, 1, 1);
if (d1 < d2) {return -1;}
else if (d1 > d2) {return 1;}
else {return 0;}
}
#endregion
}
Finally, hook up your ListView columns like this:
MyListViewSorter Sorter = new NumberListViewSorter();
Sorter.ColumnIndex = MyColumnIndex;
this.oListView.ListViewItemSorter = Sorter;
this.oListView.Sort();
The above can be put into a column click event handler with a switch/case for setting the ListViewItemSorter to your appropriate sorter class depending on what datatype is in that particular column.
~~Bonnie Berent [C# MVP] - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | BonnieB Saturday, September 12, 2009 3:57 AM |
Hi,
May be this link will help,
Regards,
Vinil;
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | VinilV Friday, September 04, 2009 1:50 PM | ListView.Sorted = true Kenneth | | Kenneth Haugland Friday, September 04, 2009 7:38 PM | hai,
thanks for reply,,,but i have to sort automatcically by first column without clicking the column of listview | | Elby Paul Saturday, September 05, 2009 5:26 AM | hi,
Using this code items get sorted but problem is my values in the list view is number like 1,2,10,50,5,6,60
now how it is sorting is 1,10,2,5,50,6,60
how i wANT is 1,2,5,6,10,50,60
what i do
| | Elby Paul Saturday, September 05, 2009 5:56 AM | Hi,
You can set sorting property of listview:
listView1.Sorting = SortOrder.Descending;
ListView.Sorting Property
Gets or sets the sort order for items in the control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.sorting.aspx
Sorting ListView Items by Column Using Windows Forms
http://msdn.microsoft.com/en-us/library/ms996467.aspx#sorting_ascendingdescending
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | Ling Wang Friday, September 11, 2009 11:57 AM | You'll need some sorting classes, basically classes that implement the IComparer interface. I start with an abstract class:
public abstract class MyListViewSorter: IComparer
{
private int m_ColumnIndex = 0;
#region Properties
public int ColumnIndex
{
get {return this.m_ColumnIndex;}
set {this.m_ColumnIndex = value;}
}
#endregion
#region Methods
public virtual int Compare(object x, object y)
{
return 0;
}
#endregion
}
I then have a sorter class for all the types of columns I might have in my ListView. They all simply override the Compare method in the base class. Here's the basic 3: string, number, date:
public class StringListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
string s1, s2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
return string.Compare(s1, s2);
}
#endregion
}
public class NumberListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
Decimal i1, i2;
string s1, s2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
if (s1.Trim().Length == 0)
{
i1 = 0;
}
else
{
try
{i1 = Decimal.Parse(s1);}
catch (Exception)
{i1 = 0;}
}
if (s2.Trim().Length == 0)
{
i2 = 0;
}
else
{
try
{i2 = Decimal.Parse(s2);}
catch (Exception)
{i2 = 0;}
}
if (i1 < i2) {return -1;}
else if (i1 > i2) {return 1;}
else {return 0;}
}
#endregion
}
public class DateTimeListViewSorter: MyListViewSorter
{
#region Methods
public override int Compare(object x, object y)
{
ListViewItem row1, row2;
string s1, s2;
DateTime d1, d2;
row1 = (ListViewItem)x;
row2 = (ListViewItem)y;
s1 = row1.SubItems[this.ColumnIndex].Text;
s2 = row2.SubItems[this.ColumnIndex].Text;
if (s1.Trim().Length > 0)
d1 = DateTime.Parse(s1);
else
d1 = new DateTime(1900, 1, 1);
if (s2.Trim().Length > 0)
d2 = DateTime.Parse(s2);
else
d2 = new DateTime(1900, 1, 1);
if (d1 < d2) {return -1;}
else if (d1 > d2) {return 1;}
else {return 0;}
}
#endregion
}
Finally, hook up your ListView columns like this:
MyListViewSorter Sorter = new NumberListViewSorter();
Sorter.ColumnIndex = MyColumnIndex;
this.oListView.ListViewItemSorter = Sorter;
this.oListView.Sort();
The above can be put into a column click event handler with a switch/case for setting the ListViewItemSorter to your appropriate sorter class depending on what datatype is in that particular column.
~~Bonnie Berent [C# MVP] - Marked As Answer byLing WangMSFT, ModeratorSunday, September 13, 2009 9:26 AM
-
| | BonnieB Saturday, September 12, 2009 3:57 AM |
|