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?
dodgerfanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>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 ?
0
Omer RasheedCommented:
@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.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

dodgerfanAuthor 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.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>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.
0
dodgerfanAuthor 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.
0
dodgerfanAuthor 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?
0
AndyAinscowFreelance programmer / ConsultantCommented:
foreach (var item in documents)
{
      lstMyList.Items.Clear();    <<-----------  Here you remove anything added when you go through all the items

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

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dodgerfanAuthor Commented:
That got it. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.