简体   繁体   English

在C#Windows窗体中将组和项添加到ListView

[英]Adding groups and items to ListView in C# windows form

I'm trying to create a ListView in Windows Form that contains groups and items i get from DataBase. 我试图在Windows窗体中创建一个ListView,其中包含从数据库中获取的组和项目。

My ListView is called "lstItems" 我的ListView称为“ lstItems”

In the begining, the ListView is empty and I fill it with data during the runnig of the program. 首先,ListView是空的,我在程序运行期间用数据填充它。

This is the code I use to create the groups: 这是我用来创建组的代码:

foreach(DataRow r in tasksTbl.Rows)
{
    string groupName = "group" + num;
    num++;
    lstItems.Groups.Add(groupName, r.Field<string>(0));
}

The tasksTbl table is not empty and it creates several group that I cannot see on the screen at this point. taskTbl表不为空,它创建了一些我目前无法在屏幕上看到的组。

This is the code I use to create the items and subItems for the groups: 这是我用来为组创建项目和子项目的代码:

private void CreateItem(DataTable tbl)
{
    int taskId = tbl.Rows[0].Field<int>(0);
    string taskName = tbl.Rows[0].Field<string>(1);
    DateTime startDate = tbl.Rows[0].Field<DateTime>(2);
    DateTime endDate = tbl.Rows[0].Field<DateTime>(3);

    string dateStr = startDate.ToString() + " - " + endDate.ToString();

    ListViewItem item = new ListViewItem(dateStr);
    item.Tag = taskId.ToString();

    foreach (DataRow r in tbl.Rows)
    {
        string position = r.Field<string>(5);
        string soldier = r.Field<string>(6);
        item.SubItems.Add(soldier + " (" + position + ")");
    }

    foreach(ListViewGroup grp in lstItems.Groups)
        if (grp.Header.Equals(taskName))
            grp.Items.Add(item);
}

Here also the tbl table is not empty and it creates the items and sub items to each group. 这里的tbl表也不为空,它为每个组创建项目和子项目。

I can see in the debugger that the groups has the items properly. 我可以在调试器中看到组中的项目正确。

My problem is that I cannot see the groups or the items on the screen. 我的问题是我无法在屏幕上看到组或项目。

What am I missing? 我想念什么?

Can someone give me a hand? 有人可以帮我吗?

Thank you in advance! 先感谢您!

itzick, itzick,

You need to create the groups as you go and assign them to the items you add to the ListView Control. 您需要随时创建组,并将它们分配给添加到ListView控件的项目。 Here is a simple example which loads a ListView with the numbers 65 to 76. The groups are based upon the number modulus 5. 这是一个简单的示例,该示例加载具有65到76的数字的ListView。这些组基于数字模数5。

Create a form, add a ListView called listView1, add the method below and call that method during form load. 创建一个表单,添加一个名为listView1的ListView,添加以下方法,并在表单加载期间调用该方法。 You should see a ListView with five groups and a few member items in each group. 您应该看到一个具有五个组的ListView,每个组中有几个成员项。

    private void LoadListView()
    {
        // Assume we are in a form, with a ListView control called listView1 on the form

        // Create a group label array
        var groupLabels = new string[5];
        groupLabels[0] = "aaa";
        groupLabels[1] = "bbb";
        groupLabels[2] = "ccc";
        groupLabels[3] = "ddd";
        groupLabels[4] = "eee";

        for (var i = 65; i < 76; i++)
        {
            // Find group or create a new group
            ListViewGroup lvg = null;
            var found = false;
            foreach (var grp in listView1.Groups.Cast<ListViewGroup>().Where(grp => grp.ToString() == groupLabels[i % 5]))
            {
                found = true;
                lvg = grp;
                break;
            }

            if (!found)
            {
                // Group not found, create
                lvg = new ListViewGroup(groupLabels[i % 5]);
                listView1.Groups.Add(lvg);
            }

            // Add ListViewItem
            listView1.Items.Add(new ListViewItem {Text = i.ToString(CultureInfo.InvariantCulture), Group = lvg});
        }

I figured out my problem. 我发现了我的问题。

I needed to add columns to the ListView and then, to add the items to the ListView and only in the end to add the items to the groups. 我需要将列添加到ListView,然后将项目添加到ListView,最后才将项目添加到组。

I did it and now it works. 我做到了,现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM