adding items to columns/rows in listview using foreach

∥☆過路亽.° 提交于 2020-01-25 03:46:05

问题


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 });
}

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?


回答1:


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

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);
        }

I have simplified the code for your understanding, since there several ways to iterate through a Dictionary...

Hope it helps!!




回答2:


You do this almost exactly the same as in C. Just loop through the collection...

int i = 0;
foreach (var column in listValues)
{
    var item = new ListViewItem("column " + i++);
    foreach (var row in column)
    {
        item.SubItems.Add(row);
    }        
    listView1.Items.Add(item);
}

It's hard to provide a real example without seeing what your collection looks like, but for an array of arrays this will work.



来源:https://stackoverflow.com/questions/11391225/adding-items-to-columns-rows-in-listview-using-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!