We help IT Professionals succeed at work.

Get the selected value in multi column listview

dodgerfan
dodgerfan asked
on
117 Views
Last Modified: 2018-09-21
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?
Comment
Watch Question

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
>>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.

Author

Commented:
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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
>>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.

Author

Commented:
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.

Author

Commented:
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?
Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
That got it. Thank you.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions