Get the selected value in multi column listview

dodgerfan
dodgerfan used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

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

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

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

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
Commented:
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);
}

Author

Commented:
That got it. Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial