:정렬 항목에 ListView를, 그리고 그 후 - 하위 항목이 같은 내 항목과 하위 항목을 정렬 할 1
one - a
one - b
one - c
three - apricot
three - banana
two - orange
two - pear
내가 오름차순으로 정렬하지만 설정 한 경우에만 첫 번째 열을 정렬합니다.
:정렬 항목에 ListView를, 그리고 그 후 - 하위 항목이 같은 내 항목과 하위 항목을 정렬 할 1
one - a
one - b
one - c
three - apricot
three - banana
two - orange
two - pear
내가 오름차순으로 정렬하지만 설정 한 경우에만 첫 번째 열을 정렬합니다.
사용자 정의 IComparer
을 제공하려면 ListView.ListViewItemSorter
속성을 사용해야합니다. ListViewItem
에 액세스 할 수 있으며 Compare
방법의 하위 항목입니다.
다음은이를 수행하는 예입니다 (함께 던집니다). 어떤 부분이 샘플 ListView
을 설정하고 있는지 설명하는 코드의 주석에 유의하십시오. 원하는 정렬을 실제로 구현하는 데 필요한 코드가 거의없고, 해당 위치에 주석을 달았습니다. 코드에없는 유일한 설정은 새 빈 WinForm에 ListView
을 넣는 것입니다. 나머지는 코드에서 수행되며 그 결과를 보여주는 샘플 이미지가 있습니다. 위의 코드 (항목들이 추가 된 순서에없는주의를) 실행
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SuspendLayout();
// This part is just setting up the ListView
// Turn off default sorting, and set to display columns
this.listView1.Sorting = SortOrder.None;
this.listView1.View = View.Details;
// Add two columns (ColumnA and ColumnB)
this.listView1.Columns.Add(new ColumnHeader());
this.listView1.Columns[0].Width = 100;
this.listView1.Columns[0].Text = "ColumnA";
this.listView1.Columns.Add(new ColumnHeader());
this.listView1.Columns[1].Width = 100;
this.listView1.Columns[1].Text = "ColumnB";
// Add the actual column information
ListViewItem listViewItem1 = new ListViewItem(new String[] {"three", "banana"});
ListViewItem listViewItem2 = new ListViewItem(new String[] {"one", "c"});
ListViewItem listViewItem3 = new ListViewItem(new String[] {"one", "b"});
ListViewItem listViewItem4 = new ListViewItem(new String[] {"three", "apricot"});
this.listView1.Items.AddRange(new ListViewItem[]{listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4});
/*
Now the actual sorting - this next line makes it sort using
the custom comparer we've defined. Assigning a new IComparer
automatically sorts using it.
*/
this.listView1.ListViewItemSorter = new ListViewItemComparer();
this.ResumeLayout(false);
}
}
// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer
{
public ListViewItemComparer()
{
}
// This function actually does the comparison
public int Compare(object x, object y)
{
/*
We need to access the same item multiple times, so
save a local reference to reduce typecasting over and
over again
*/
ListViewItem FirstItem = (ListViewItem) x;
ListViewItem SecondItem = (ListViewItem) y;
/*
Compare the two columns of each item, combined to make
a single item for comparing.
*/
return String.Compare(FirstItem.SubItems[0].Text + FirstItem.SubItems[1].Text,
SecondItem.SubItems[0].Text + SecondItem.SubItems[1].Text);
}
}
}
결과 :
우리가 정렬 무엇감사합니다, 내 모든 "public class ListViewSortForm : Form"내 Form1에 붙여 넣습니다. 나는 그것의 맨 끝에 "class ListViewItemComparer : IComparer"를 넣었다. 그러나 "메서드 반환 형식이 있어야합니다"말하는 오류가 발생합니다. 나는 C#에서의 진정한 초보자이기 때문에 조금 느리다면 실례합니다. 이것은 내 C# 프로젝트입니다. http://spunit.tk/x/PassPlace.rar – spunit
감사합니다. 나는 그것을 알아 내기 위해 노력할 것이다. – spunit
@spunit : 샘플 코드를 다시 작성하고 이미지를 추가했습니다. 새로운 텍스트와 코드 주석을주의 깊게 읽으십시오. –
? 문자열 목록? 그렇다면 2 개의 열이 없습니다. 문자열이 아니라면 무엇입니까? 제발 더 많은 코드. –
일반 ListView입니다. 거기에 문자열 항목이 있고 항목에 문자열 하위 항목이 있습니다. 이렇게 : http://spunit.tk/x/scrn2_3.png – spunit