Finding a item in listview and changing the index

P

plmanikandan

Hi,
I have a listview with items,subitems.I
tems and subitems are added as below

listView1.View = View.Details;
listView1.Columns.Add("Column 1", 100,HorizontalAlignment.Center);
listView1.Columns.Add("Column 2", 100,HorizontalAlignment.Center);
listView1.Columns.Add("Column 3", 100,HorizontalAlignment.Center);
listView1.Columns.Add("Column 4", 100,HorizontalAlignment.Center);
System.Windows.Forms.ListViewItem itmp = new
System.Windows.Forms.ListViewItem("PARENT Item");
itmp.BackColor = System.Drawing.Color.Silver;
itmp.ForeColor = System.Drawing.Color.Navy;
System.Windows.Forms.ListViewItem.ListViewSubItem itms1 = new
System.Windows.Forms.ListViewItem.ListViewSubItem(itmp, "SubItem 1");

System.Windows.Forms.ListViewItem.ListViewSubItem itms2 = new
System.Windows.Forms.ListViewItem.ListViewSubItem(itmp, "SubItem 2");
System.Windows.Forms.ListViewItem.ListViewSubItem itms3 = new
System.Windows.Forms.ListViewItem.ListViewSubItem(itmp, "SubItem 3");

itmp.SubItems.Add(itms1);
itmp.SubItems.Add(itms2);
itmp.SubItems.Add(itms3);
listView1.Items.Add(itmp);
System.Windows.Forms.ListViewItem itmp1 = new
System.Windows.Forms.ListViewItem("PARENT Item");
int r=listView1.Items.IndexOf(itmp1);
MessageBox.Show(r.ToString());

when i serach for PARENT Item it is always giving result as -1(item not
found)

I need to find an item in the listview and then change that item to
appear first(index=0) in listview[make the item to appear first in
listview].Kindly help me to solve the problem.I'm using net
framework1.1(C#)

Thanks & Regards,
Manikandan
 
C

Claes Bergefall

System.Windows.Forms.ListViewItem itmp1 = new
System.Windows.Forms.ListViewItem("PARENT Item");
int r=listView1.Items.IndexOf(itmp1);
MessageBox.Show(r.ToString());

when i serach for PARENT Item it is always giving result as -1(item not
found)

That's because you're not searching for the original "Parent" item. In the
above code you create a new item with the same text as the one you're
looking for, but it's not the same *item* so you won't find it since it
hasn't been added to the listview. IndexOf looks for the *exact* instance
that you're passing to it, not some item that happen to have the same text.
You will have to loop all your items and check the text until you find the
one you're looking for.
I need to find an item in the listview and then change that item to
appear first(index=0) in listview[make the item to appear first in
listview].Kindly help me to solve the problem.I'm using net
framework1.1(C#)

ListViewItem item = listView1.Items[index];
listView1.Items.RemoveAt(index);
listView1.Items.Insert(0, item)


/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top