Reading ListView string[] items

R

Ray Mitchell

Hello,

In the following code assume that the ListView object "myListView" has been
properly created. What I am attempting to do is add a string[] as a list
view item, then read the various string elements from the list view item. I
assume I am adding the string[] item properly, but I get the error
indicatede when I try to read one of the string[]'s elements from the
ListView item. What am I missing?

public void Test()
{
ListView.ListViewItemCollection itemCollection =
new ListView.ListViewItemCollection(myListView);

// for use as a ListView item
string[] record = new string[10];

// create a ListView item
ListViewItem lvItem = new ListViewItem(record);

// add item to ListView
itemCollection.Add(lvItem);

// Try to read element 0 of the string[] in the ListView item at index 0
// Error: Cannot convert type 'System.Windows.Forms.ListViewItem' to
'string[]'
string itemField = ((string [])itemCollection[0])[0];
}

Thanks,
Ray Mitchell
(e-mail address removed)
 
M

Morten Wennevik

ItemCollection[0] returns a ListViewItem, a single item, and when you pass
it a string[] I'm not sure what happens. If it fills the ListViewItem in
columns, what you want is

string itemField = itemCollection[0].SubItems[0];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top