Copying the content of a Listview to an array.- Or how can I select a row in my program?

Posted on 2005-04-02
Medium Priority
Last Modified: 2010-04-24

I have 3 columns of data I read in from a file to an array and then load into a Listview using the Add function.

I then change, add or delete some items in the Listview. Finally I would like to dump the Listview content bask to the
array and then to a file.

However, I have not been able to find a way to move the content from the Listview to the array. Mainly because
I cannot find how to select an item from my program.

If I could just select e.g. Item [0], then I could loop through the Listview and then copy the subitems to my arrays.

I look forward to hear any suggestions.

Best regards

Poul Thomsen
Question by:PoulHThomsen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 13689736
You do not need to select a list item to extract the list items' data from a ListView.

The simplified object model for ListView is:

ListView ---> (1) ListViewItemCollection ---> (*) ListViewItem ---> (1) ListViewSubItemCollection ---> (*) ListViewSubItem

In actual fact, a ListView maintains several ListViewItemCollection (not just one, as shown above) - there's one that contains all items, one that contains selected items, and one for checked items. But, for the purposes of extracting all items - which is what I understand you want - I simplified the object model a bit.

You get the ListViewItemCollection containing all items (rows) in a ListView through the ListView.Items property. The collection can be navigated like any other collection (including foreach). Each item in this collection is a ListViewItem.
You get the ListViewSubItemCollection containing all sub-items (columns) through the ListViewItem.SubItems property. Once again, you can navigate this collection in the usual way. Each item in this collection is a ListViewSubItem.

However, since you already populate the ListView, you would've been presumably aware of all this. So, it's possible I have misunderstood your question.


Author Comment

ID: 13690817
Hello Radu and thanks for your answer.

I must admit that I am quite a beginner to this platform and language, so my code for this application are
based on examples and tutorials e.g. in the Visual Studio Guide comming with the Software.

However, I find it difficult to find my way around in the help files describing e.g. the listview class. The same
'sample code' where you can populate the listview is repeated many times, but that is it.

But there is no information about how to navigate in the listview in a program.

I would be grateful if you could provide a codesnippet or a reference to your solutions.

Best regards

Poul Thomsen

Accepted Solution

rcarlan earned 1000 total points
ID: 13691159
Hi Poul,

I feel for your predicament. Doing .NET programming in C++ is not the easiest or most natural thing to do.

I do all my .NET programming in C#. Unless you have constraints that force you to use the managed extensions for C++, I would advise to give C# a go. It has been specifically designed for .NET programming. Coming from C++ you'll find it takes no time at all to learn C#. And you can still do your low level programming in C++, if you so desire.

Anyway, the following code snippet shows how to navigate the ListView object model:

System::Windows::Forms::ListView* pListView = GetMyListView();   //  replace this with your access to the ListView control
System::Windows::Forms::ListView::ListViewItemCollection* pLVItems = pListView->Items;
for (int i=0; i<pLVItems->Count; ++i)
      System::Windows::Forms::ListViewItem* pLVItem = pLVItems->Item[i];
      System::Windows::Forms::ListViewItem::ListViewSubItemCollection* pLVSubItems = pLVItem->SubItems;
      for (int j=0; j<pLVSubItems->Count; ++j)
            System::Windows::Forms::ListViewItem::ListViewSubItem* pLVSubItem = pLVSubItems->Item[j];
            System::String* psCellText = pLVSubItem->Text;      // this is the text in the corresponding row-column cell

Now, the same code in C#:

using System.Windows.Forms;

ListView listView = GetMyListView();   //  replace this with your access to the ListView control
foreach (ListViewItem lvItem in listView.Items)
      foreach (ListViewItem.ListViewSubItem lvSubItem in lvItem.SubItems)
            string cellText = lvSubItem.Text;

I cheated a bit. I could have obviously used a using statement in the C++ code, too.
Even so, you have to admit the C# code is a lot clearer (and less to type, too).


Author Comment

ID: 13691253
Hello Radu.

That did it indeed.

Thanks a lot for your help.

I wish I started this in C#. In case I run into more big problems, I will evaulate to change language. But at this time I have the most of the functions in my program working. This was one of the last 'hurdles'

Thanks again. I will now see how I can stamp you answer as accepted.

Best regards

Poul Thomsen.

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question