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?
Re: Getting index of a column in a ListView
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.
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;
}
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;
}