使用的foreach项目添加到列/行列表视图中 [英] adding items to columns/rows in listview using foreach

查看:125
本文介绍了使用的foreach项目添加到列/行列表视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到学习c#的第5天,并正尝试找出如何填补/重新填充ListView控件,含10行12列,使用foreach循环。我有$ C $的CD,我的功能用C之后。

I am into day 5 of learning c#, and am trying to figure out how to fill/re-fill a ListView control, containing 10 rows and 12 columns, using a foreach loop. I have coded the functionality I'm after in C.

void listPopulate(int *listValues[], int numberOfColumns, int numberOfRows)
{
    char table[100][50];
    for (int columnNumber = 0; columnNumber < numberOfColumns; ++columnNumber)
    {
        for (int rowNumber = 0; rowNumber < numberOfRows; ++rowNumber)
        {
            sprintf(&table[columnNumber][rowNumber], "%d", listValues[columnNumber][rowNumber]);
            // ...
        }
    }
}

这是我迄今想通了:

Here is what I have figured out so far:

public void listView1_Populate()
{

    ListViewItem item1 = new ListViewItem("value1");
    item1.SubItems.Add("value1a");
    item1.SubItems.Add("value1b");

    ListViewItem item2 = new ListViewItem("value2");
    item2.SubItems.Add("value2a");
    item2.SubItems.Add("value2b");

    ListViewItem item3 = new ListViewItem("value3");
    item3.SubItems.Add("value3a");
    item3.SubItems.Add("value3b");
    ....

    listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
}

我假设我会做一个单独的步骤列表项的创建。所以我的问题是:必须有一种用for或foreach循环做这在C#中,没有

I'm assuming that I would have to do the creation of the list items in a separate step. So my question is: there must be a way to do this in C# with a for or foreach loop, no?

推荐答案

我不知道如果我理解正确,但这里是我认为你需要什么...

I am not sure if I understood you correctly, but here's i think what you need...

其实这取决于你的数据源您正在使用,填补了的ListView
像这样的东西(我使用 Dictioanry 这里一个DataSource) -

Actually it depends on your DataSource which you are using to fill up the ListView. Something like this (I am using Dictioanry as a DataSource here) -

        // Dictinary DataSource containing data to be filled in the ListView
        Dictionary<string, List<string>> Values = new Dictionary<string, List<string>>()
        {
            { "val1", new List<string>(){ "val1a", "val1b" } },
            { "val2", new List<string>(){ "val2a", "val2b" } },
            { "val3", new List<string>(){ "val3a", "val3b" } }
        };

        // ListView to be filled with the Data
        ListView listView = new ListView();

        // Iterate through Dictionary and fill up the ListView
        foreach (string key in Values.Keys)
        {
            // Fill item
            ListViewItem item = new ListViewItem(key);

            // Fill Sub Items
            List<string> list = Values[key];
            item.SubItems.AddRange(list.ToArray<string>());

            // Add to the ListView
            listView.Items.Add(item);
        }

我已经简化了code的理解,因为有几种方式通过迭代一个词典 ...

希望它可以帮助!

这篇关于使用的foreach项目添加到列/行列表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆