CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Post Getting index of a column in a ListView

    Hi,

    I am looking around for a way to get the index of a column by the name of it. The problem I want to solve is the manipulation of subitems in a ListView. At the moment I do it in that way:
    Code:
    foreach (ListViewItem lvi in myListView.SelectedItems)
      lvi.SubItems[4].Text = "myText";
    The problem is that I have to select by a number. If I add a column later and reorder the columns I have to replace all numbers in the source code. I think there is a way to get the index at runtime from the column by the name of the column.

    Any suggestions?
    Useful or not? Rate my posting. Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: Getting index of a column in a ListView

    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  3. #3
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Getting index of a column in a ListView

    Well, good to know this article, but it do not solve my problem. Because in the article a column header occures and so the author knows which column was clicked and can get the index. That is the problem for me, I only have the name of the column and have to get the column by it.
    Useful or not? Rate my posting. Thanks.

  4. #4
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Getting index of a column in a ListView

    Code:
    int iCurSelIndex = -1;
    foreach (ListViewItem lvi in this.LVClients.SelectedItems)
        foreach (ListViewItem.ListViewSubItem lvisub in lvi.Subitems)
            if (lvisub.Text == "myText")
            {
                iCurSelIndex = lvi.Index;
                break;
            }
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  5. #5
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Getting index of a column in a ListView

    oops Toruud!
    sorry wrong code I thinnk.... this is what you are looking for... most probably....

    Code:
    int iCurSelIndex = -1;
    string szColName = string.Empty;
    foreach (ListViewItem lvi in this.LVClients.SelectedItems)
        foreach (ListViewItem.ListViewSubItem lvisub in lvi.Subitems)
            if (lvisub.Text == "myText")
            {
                iCurSelIndex = lvi.SubItems.IndexOf(lvisub);
                szColName = LVClients.Columns[iCurSelIndex].Name;
                szColName = LVClients.Columns[iCurSelIndex].Text;
                iCurSelIndex = lvi.Index;
                break;
            }
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured