|
-
March 14th, 2006, 04:37 AM
#1
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.
-
March 14th, 2006, 04:50 AM
#2
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!!
-
March 14th, 2006, 05:17 AM
#3
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.
-
March 15th, 2006, 08:35 AM
#4
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.
-
March 15th, 2006, 08:38 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|