Listview小组工作得很好

时间:2015-09-21 14:16:53

标签: c# winforms listview

在我的应用中,我会显示一个带有几个条目的listview。我可以按群组对它们进行分组,效果很好。

当我通过我创建的API(基于相同的代码)在Excel / MatLab中执行相同操作时,这些组无法正常工作,请参阅图片:

在申请中:

enter image description here

在API中:

enter image description here

正如您所看到的,它不起作用。这是我使用的代码:

// Sets the ListView to the groups created for the specified column. 
private void SetGroups(Hashtable groups,int column)
{            
    // Remove the current groups.
    listView1.Groups.Clear();

    // 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 the ListView.
    Array.Sort(groupsArray, new ListViewGroupSorter(listView1.Sorting));
    listView1.Groups.AddRange(groupsArray);

    // Iterate through the items in the ListView, assigning each  
    // one to the appropriate group. 
    foreach (ListViewItem item in listView1.Items)
    {
        // Retrieve the subitem text corresponding to the column. 
        string subItemText = item.SubItems[column].Text;

        // For the Title column, use only the first 3 letters. 
        if (column == 0)
        {
            subItemText = subItemText.Substring(0,3);
        }

        // Assign the item to the matching group.
        item.Group = (ListViewGroup) groups[subItemText];
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Application.EnableVisualStyles();放在表单的构造函数中:

public Form1()
{
    InitializeComponent();
    Application.EnableVisualStyles();
}