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?
C#

Avatar of undefined
Last Comment
dodgerfan
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.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>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 ?
Avatar of Omer Rasheed
Omer Rasheed
Flag of Pakistan image

@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
dodgerfan
Flag of United States of America image

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.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>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.
Avatar of dodgerfan
dodgerfan
Flag of United States of America image

ASKER

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.
Avatar of dodgerfan
dodgerfan
Flag of United States of America image

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dodgerfan
dodgerfan
Flag of United States of America image

ASKER

That got it. Thank you.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo