Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. ListView move Up / Down using Index ??

ListView move Up / Down using Index ??

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luckyShek
    wrote on last edited by
    #1

    :omg:I have a Form with a ListView and two buttons(Move_Up & Move_Down). I want to reoder the ListView by moving an item up or down by clicking either the button Up / button Down. I get the selected listView item's Index, then use Index + 1 or Index -1 to move the item up / down. The code of move up with Index-1 works. But the code of move down with Index+1 does not work. I don't know why, please help! Thanks! The code is as follows: // this move up works (Index-1) private void btnUp_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index -1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } } // this move down does NOT work (Index +1) private void btnDown_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index + 1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } }

    F 1 Reply Last reply
    0
    • L luckyShek

      :omg:I have a Form with a ListView and two buttons(Move_Up & Move_Down). I want to reoder the ListView by moving an item up or down by clicking either the button Up / button Down. I get the selected listView item's Index, then use Index + 1 or Index -1 to move the item up / down. The code of move up with Index-1 works. But the code of move down with Index+1 does not work. I don't know why, please help! Thanks! The code is as follows: // this move up works (Index-1) private void btnUp_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index -1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } } // this move down does NOT work (Index +1) private void btnDown_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index + 1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } }

      F Offline
      F Offline
      Fade Amit BS
      wrote on last edited by
      #2

      It doesn't work becuase.... oh, i'll just show you The list: Item1="1" Item2="2" Item3="3" OK? now lets say you want to move the item at index 2 one step down, your code does two things, add a new item, and removes the old one, right? lest do these two steps- Step 1 - add new item Item1="1" Item2="2" Item3="2" Item4="3" Now if we remove item at index 2+1 (item3 now) what do you think will happen? what you need to do is switchi between the item(index) and item (index+1) the same thing will work with moving an item upwards - switch item(index) with item(index-1) it will be a lot faster then using the insert/b> method, trust me :) Fade (Amit BS)

      A 1 Reply Last reply
      0
      • F Fade Amit BS

        It doesn't work becuase.... oh, i'll just show you The list: Item1="1" Item2="2" Item3="3" OK? now lets say you want to move the item at index 2 one step down, your code does two things, add a new item, and removes the old one, right? lest do these two steps- Step 1 - add new item Item1="1" Item2="2" Item3="2" Item4="3" Now if we remove item at index 2+1 (item3 now) what do you think will happen? what you need to do is switchi between the item(index) and item (index+1) the same thing will work with moving an item upwards - switch item(index) with item(index-1) it will be a lot faster then using the insert/b> method, trust me :) Fade (Amit BS)

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Thanks a lot.;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups