ListViewItem.Group Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur grup tempat item ditetapkan.
public:
property System::Windows::Forms::ListViewGroup ^ Group { System::Windows::Forms::ListViewGroup ^ get(); void set(System::Windows::Forms::ListViewGroup ^ value); };
public System.Windows.Forms.ListViewGroup Group { get; set; }
public System.Windows.Forms.ListViewGroup? Group { get; set; }
member this.Group : System.Windows.Forms.ListViewGroup with get, set
Public Property Group As ListViewGroup
Nilai Properti
tempat ListViewGroup item ditetapkan.
Contoh
Contoh kode berikut menunjukkan bagaimana Group properti dapat digunakan dalam aplikasi yang mengatur ListView item berdasarkan nilai subitem dalam tampilan detail. Bentuk pengelompokan ini mirip dengan pengelompokan yang digunakan di Windows Explorer. Dalam contoh, grup dibuat secara dinamis. Untuk setiap kolom subitem, satu grup dibuat untuk setiap nilai subitem unik. Untuk kolom item induk, satu grup dibuat untuk setiap huruf awal yang unik. Grup yang dibuat untuk setiap kolom disimpan dalam tabel hash bersama dengan teks subitem atau huruf awal. Saat header kolom diklik, tabel hash yang sesuai dengan kolom tersebut diambil. Selanjutnya, nilai teks subitem untuk kolom tersebut digunakan sebagai kunci tabel hash untuk mengambil grup yang benar untuk setiap item. Item kemudian ditetapkan ke grup menggunakan Group properti .
Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk ListView.Groups properti .
// Sets myListView to the groups created for the specified column.
private:
void SetGroups(int column)
{
// Remove the current groups.
myListView->Groups->Clear();
// Retrieve the hash table corresponding to the column.
Hashtable^ groups = dynamic_cast<Hashtable^>(groupTables[column]);
// Copy the groups for the column to an array.
array<ListViewGroup^>^ groupsArray = gcnew array<ListViewGroup^>(groups->Count);
groups->Values->CopyTo(groupsArray, 0);
// Sort the groups and add them to myListView.
Array::Sort(groupsArray, gcnew ListViewGroupSorter(myListView->Sorting));
myListView->Groups->AddRange(groupsArray);
// Iterate through the items in myListView, assigning each
// one to the appropriate group.
IEnumerator^ myEnum = myListView->Items->GetEnumerator();
while (myEnum->MoveNext())
{
ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current);
// Retrieve the subitem text corresponding to the column.
String^ subItemText = item->SubItems[column]->Text;
// For the Title column, use only the first letter.
if (column == 0)
{
subItemText = subItemText->Substring(0, 1);
}
// Assign the item to the matching group.
item->Group = dynamic_cast<ListViewGroup^>(groups[subItemText]);
}
}
// Sets myListView to the groups created for the specified column.
private void SetGroups(int column)
{
// Remove the current groups.
myListView.Groups.Clear();
// Retrieve the hash table corresponding to the column.
Hashtable groups = (Hashtable)groupTables[column];
// Copy the groups for the column to an array.
ListViewGroup[] groupsArray = new ListViewGroup[groups.Count];
groups.Values.CopyTo(groupsArray, 0);
// Sort the groups and add them to myListView.
Array.Sort(groupsArray, new ListViewGroupSorter(myListView.Sorting));
myListView.Groups.AddRange(groupsArray);
// Iterate through the items in myListView, assigning each
// one to the appropriate group.
foreach (ListViewItem item in myListView.Items)
{
// Retrieve the subitem text corresponding to the column.
string subItemText = item.SubItems[column].Text;
// For the Title column, use only the first letter.
if (column == 0)
{
subItemText = subItemText.Substring(0, 1);
}
// Assign the item to the matching group.
item.Group = (ListViewGroup)groups[subItemText];
}
}
' Sets myListView to the groups created for the specified column.
Private Sub SetGroups(column As Integer)
' Remove the current groups.
myListView.Groups.Clear()
' Retrieve the hash table corresponding to the column.
Dim groups As Hashtable = CType(groupTables(column), Hashtable)
' Copy the groups for the column to an array.
Dim groupsArray(groups.Count - 1) As ListViewGroup
groups.Values.CopyTo(groupsArray, 0)
' Sort the groups and add them to myListView.
Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
myListView.Groups.AddRange(groupsArray)
' Iterate through the items in myListView, assigning each
' one to the appropriate group.
Dim item As ListViewItem
For Each item In myListView.Items
' Retrieve the subitem text corresponding to the column.
Dim subItemText As String = item.SubItems(column).Text
' For the Title column, use only the first letter.
If column = 0 Then
subItemText = subItemText.Substring(0, 1)
End If
' Assign the item to the matching group.
item.Group = CType(groups(subItemText), ListViewGroup)
Next item
End Sub
Keterangan
Gunakan properti ini untuk mengatur grup tempat item berada. Anda juga dapat mengatur grup di ListViewItem konstruktor, atau Anda dapat menggunakan properti ini untuk mengubah keanggotaan grup pada waktu proses. Jika Anda mengatur properti ini ke null dan ada grup dalam ListView.Groups koleksi, item akan muncul di grup default, yang memiliki label header "DefaultGroupSystem.Windows.Forms". Grup default tidak terkandung dalam ListView.Groups koleksi, dan tidak dapat diubah. Ini terutama berguna dalam penelusuran kesalahan untuk memastikan bahwa semua item telah ditambahkan dengan benar ke grup.
Nota
ListView grup hanya tersedia di Windows XP dan keluarga Windows Server 2003 (Windows XP Home Edition, Windows XP Professional, Windows Server 2003). Untuk informasi selengkapnya, lihat ListViewGroup topik gambaran umum.