Solved

Selecting a ListView column by name

Posted on 2006-07-20
2
247 Views
Last Modified: 2010-04-23
Hi all,
I have a dynamically populated ListView in my app and I need to be able to specify which column I want to perform changes on. The only problem is that I don't know the columns index number and even if I did it could have changed. The only thing I do know it the column header name. Is there anyway I can specify a column in my code using the column name rather than the index?
Thanks
0
Comment
Question by:kiranboi
  • 2
2 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17145576
Hi kiranboi,

Why dont you create a function to loop through the columns and return the index:

public int GetIndexByName( string colName )
{
    foreach( ColumnHeader header in YourListView.Columns )
         {
            if ( header.Text == colName )
            {
               return header.Index;
            }
         }
}

Apresto
0
 
LVL 23

Accepted Solution

by:
apresto earned 500 total points
ID: 17145583
sorry, more correctly:

public int GetIndexByName( string colName )
{
    foreach( ColumnHeader header in lstCustomers.Columns )
    {
       if ( header.Text == colName )
       {
          return header.Index;
       }
    }
    return -1;
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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