Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

Get the selected value in multi column listview

I need to get the selected value of a listview that has 2 columns. I think this should be simple, but I am not getting the value I need. I have a winforms application, in C#. I populate the list view like this:
foreach (var item in documents)
{
	lstMyList.Items.Add(item.Name + "\t\t" + item.Value + "\t\t");
}

Open in new window

It displays the list with the values side by side. When I select a value from the list, I want to capture just the item.Name value. How can I do that?
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi dodgerfan,

You can always put a separator between item.Name and item.Value and then split from that OR use Combobox that can support Display and Value parts independently.

Regards,
Chinmay.
>>I need to get the selected value of a listview that has 2 columns

First that is only one column which contains a string which displays:-  'name' 'tab space' 'value'.

How do you try to get the name from the selected item currently ?
@dodgerfan, please elaborate your requirement.

As per your code-snippet your are filling-up list lstMyList.Items.Add(item.Name + "\t\t" + item.Value + "\t\t"); with spaces. So you can split the value and get the desire result.
Avatar of dodgerfan

ASKER

I'm not sure I'm using the right approach. I want to show a list of values in a list. I'm using a listview but have been trying a listbox, too. I think listview is the way to go. The listview will display a list of values, each with 2 columns, Name and Value. I am putting them together in one string and trying to get the formatting looking good. When a value is selected, I only want to pass the Name value for whatever I call next. It sounds like I need to split my current value. Is there a better way to do what I'm trying to do? Thank you.
>>I am putting them together in one string

Why.  As you said a listview can have two columns.  Name in one and value in the other column.
I have not been able to get it to work any other way. I have tried this code:
lstMyList.View = System.Windows.Forms.View.Details;
lstMyList.FullRowSelect = true;
lstMyList.Girdlines = true;

lstMyList.Columns.Add("Name", 200);
lstMyList.Columns.Add("Value", 200);

foreach (var item in documents)
{
	lstMyList.Items.Clear();

	ListViewItem lvi = new ListViewItem(item.Name);
	lvi.SubItem.Add(item.Value);
	
	lstMyList.Items.Add(lvi);
}

Open in new window


That returns the list with the headers in place, but no data comes through. The Name and Value fields do not come through. So I have not been able to get it to work without doing it as one string. I'd prefer the 2 column approach. It may be easier to select a value from it that way.
if I add ToString() to the end of Name and Value, like this:
ListViewItem lvi = new ListViewItem(item.Name.ToString());
	lvi.SubItem.Add(item.Value.ToString());

Open in new window

I get the last record in the list of values in the list, but not any of the others. I've got it wrong in the foreach loop, I think?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That got it. Thank you.