?
Solved

Trying to get a ListViewItem with a given value

Posted on 2006-07-02
3
Medium Priority
?
429 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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