John Paul
John Paul

Reputation: 11

Populating ListView Column-wise from an array in C#

I have a structure consisting of 7 different arrays with a size of 20000 each.
I want to show each array as an individual column in the ListView.
How do I add values column-wise in ListView such that a single column shows a single array? I have 7 columns in the ListView.
I'm fairly new to C#. I do know how to fill data row-wise, but I've come across column-wise filling for the first time.
Any kind of help appreciated.

Upvotes: 0

Views: 2083

Answers (2)

Ilya I. Margolin
Ilya I. Margolin

Reputation: 111

Assuming all arrays have exactly the same size, consider the following idea:

for (var i = 0; i < 20000; i++)
{ 
   AddRow(column1[i], column2[i], ...);
}

If it was a part of your question: no, list view doesn't support adding data column-wise. Neither do any native WPF or 3rd party controls I know of.

PERFORMANCE: As to performance of this solution it's not traversing 20k records that takes time, it's creating 20k wpf objects. Read up about wpf virtualization to address that.

Also keep in mind that you can easily spend weeks and months looking into faster controls and rendering technologies but what it is all ultimately boils down to is that user does not really need to see all 20k records on the screen. It is not humanly possible to comprehend such amount of information. Virtualization is just another technique to not load up stuff that user cannot see, but you could address the issue yourself, by say paging, or filters, or grouping.

WinForms: I just realized that you're not talking about WPF, you're probably talking about WinForms. There's no virtualization there, although you could probably find some 3rd party component that supports it. Consider paging then.

Upvotes: 0

John Paul
John Paul

Reputation: 11

Finally got the solution!!!

string[] temp = new string[10];
for (int i = 0; i < 20000; i++)
        {
            temp[0] = Array1[i].ToString();
            temp[1] = Array2[i].ToString();
            temp[2] = Array3[i].ToString();
            temp[3] = Array4[i].ToString();
            temp[4] = Array5[i].ToString();
            temp[5] = Array6[i].ToString();
            temp[6] = Array7[i].ToString();
            temp[7] = Array8[i].ToString();
            ListViewItem listItem = new ListViewItem(temp);
            MyListView.Items.Add(listItem);
        }

Upvotes: 1

Related Questions