Solved

Trying to get a ListViewItem with a given value

Posted on 2006-07-02
3
427 Views
Last Modified: 2010-04-16
Hi,

I populated the ListView with driverLabel strings.  And now I'd like to get the ListViewItem with a given matching string.  But Items.IndexOf() return -1 even though I know it's been added.  Am I missing anything obvious?

Thanks,
Bob

string driverLabel = driverRun.Driver.DriverLabel;
if ( driverLabel != null && driverLV.Items.Count > 0 )
{
   int driverLabelIndex = driverLV.Items.IndexOf(new ListViewItem(driverLabel));
   if ( driverLabelIndex > -1 && driverLabelIndex < driverLV.Items.Count )
   {
      driverRunsLV.Items[driverRunIndex].Selected = true;
   }
}
0
Comment
Question by:ba272
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
aaronfeng earned 500 total points
ID: 17027251
Are you trying to select a specific item in the ListView?

string driverLabel = driverRun.Driver.DriverLabel;
if(!String.IsNullOrEmpty(driverLabel)) {
      foreach (ListViewItem item in this.listView1.Items.IndexOf()) {
            if (item.Text == driverLabel) {
                  item.Selected = true;
                  this.listView1.Select();
                  break;
            }
      }
 }

You might want to check my code, I wrote it free hand.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17027265
Oops, I already saw a typo, the for loop should be like this:

   foreach (ListViewItem item in this.listView1.Items)

Not

   foreach (ListViewItem item in this.listView1.Items.IndexOf())


Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 

Author Comment

by:ba272
ID: 17028178
thanks
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question