In this article, I am describing how two data rows swap within a list view by using C#. To solve this task, I searched so many things but did not find a useful link. In the end, I develop own code which performs this task successfully.
I get the index of above selected data item and then make two variables of String and get values of selected item and above of selected item. Then assign values; give value of selected item to above of selected item and value of above selected item to selected item. To make a listviewItem object tmp and add them into listviewItem Object. And it's working. This is code for upward button for downward only change one thing; change sign of - into +. It will work for downward.
Upward Direction:
int indexs = (listview_urls.SelectedItems[0].Index) - 1;
if (indexs > -1)
{
string s = listview_urls.SelectedItems[0].SubItems[1].Text;
string S2 = listview_urls.Items[indexs].SubItems[1].Text;
listview_urls.SelectedItems[0].SubItems[1].Text = S2;
listview_urls.Items[indexs].SubItems[1].Text = s;
ListViewItem tmp = new ListViewItem();
tmp.SubItems.Add(S2);
tmp.SubItems.Add(s);
ListViewItem nextItem = listView_Scheduler.Items[indexs];
nextItem.Selected = true;
listView_Scheduler.Items[indexs + 1].Selected = false;
listView_Scheduler.Focus();
listView_Scheduler.Refresh();
}
else
{
MessageBox.Show("Sorry!Can't move further upward");
}
Downward Direction:
int indexs = (listview_urls.SelectedItems[0].Index) + 1;
if (indexs < listview_urls.Items.Count)
{
string s = listview_urls.SelectedItems[0].SubItems[1].Text;
string S2 = listview_urls.Items[indexs].SubItems[1].Text;
listview_urls.SelectedItems[0].SubItems[1].Text = S2;
listview_urls.Items[indexs].SubItems[1].Text = s;
ListViewItem tmp = new ListViewItem();
tmp.SubItems.Add(S2);
tmp.SubItems.Add(s);
ListViewItem nextItem = listView_Scheduler.Items[indexs];
nextItem.Selected = true;
listView_Scheduler.Items[indexs - 1].Selected = false;
listView_Scheduler.Focus();
listView_Scheduler.Refresh();
}
else
{
MessageBox.Show("Sorry!Can't move further Downward");
}
Try it and enjoy it.

Davinder YadavPosted Mar 14, 2015, 2:45 AM
Going into lot of comlications ! Lets get it simper ! Dim oldIndex As Integer = lsvChild.FocusedItem.Index Dim newIndex As Integer = 0 newIndex = oldIndex - 1 Dim cloneItem = lsvChild.FocusedItem.Clone lsvChild.Items.RemoveAt(oldIndex) lsvChild.Items.Insert(newIndex, cloneItem)
Davinder YadavPosted Mar 14, 2015, 2:44 AM
Going into lot of comlications ! Lets get it simper !
Davinder YadavPosted Mar 14, 2015, 2:43 AM
Going into lot of comlications ! Lets get it simper !
rezo greywordsPosted Nov 16, 2011, 10:19 PM
this rocks... thank you!!
Kevin DavisPosted Aug 30, 2011, 10:10 AM
I'm sorry, but this only moved the first subitem from the selected row, not the whole row for me anyway....
Lennie KuaheditedPosted Feb 24, 2011, 11:44 PMEdited Feb 24, 2011, 11:46 PM
Hullo Muhammad Shakir, Regarding our sample coding of retrieve data from SubItem and store in variable S: ----------- string s = listview_urls.SelectedItems[0].SubItems[1].Text; What is the result of data in variable S. ---------------- The reason I am asking is I had similar coding list below but the result is in this : ListView1SubItems: [25/10/2010} instead of just 25/10/2010. Here are my coding: txtOrderDate.Text = Convert.ToString(this.listView1.SelectedItems[0].SubItems[1]); Can you pleas share the solution with me. Thank You.
KjartanPosted Apr 17, 2009, 10:49 AM
You can clone TreeView nodes and ListView items to swap them, which is an easier solution. For example: private void btnMoveUp_Click(object sender, EventArgs e) { // swap places // Get the selected item index int selectedIndex = selectedListView.SelectedItems[0].Index; // clone the item that is moving ListViewItem cloneItem = (ListViewItem)selectedListView.Items[selectedListView.SelectedItems[0].Index].Clone(); // Save the position above the current selected node int previousIndex = selectedListView.SelectedItems[0].Index - 1; ListViewItem previousItem = selectedListView.Items[previousIndex]; // Insert at new position selectedListView.Items.Insert(previousIndex, cloneItem); // Remove the item that was at old position selectedListView.Items.RemoveAt(selectedIndex); // Restore the moved item as the selected item selectedListView.Items[selectedIndex] = previousItem; selectedListView.Items[previousIndex].Selected = true; selectedListView.Focus(); selectedListView.Refresh(); } private void btnMoveDown_Click(object sender, EventArgs e) { // swap places // Get the selected item index int selectedIndex = selectedListView.SelectedItems[0].Index; // clone the item that is moving ListViewItem cloneItem = (ListViewItem)selectedListView.Items[selectedListView.SelectedItems[0].Index].Clone(); // Save the position below the current selected node int nextIndex = selectedListView.SelectedItems[0].Index + 1; ListViewItem nextItem = selectedListView.Items[nextIndex]; // Insert at new position selectedListView.Items.Insert(nextIndex + 1, cloneItem); // Remove the item that was at old position selectedListView.Items.RemoveAt(selectedIndex); // Restore the moved item as the selected item selectedListView.Items[selectedIndex] = nextItem; selectedListView.Items[nextIndex].Selected = true; selectedListView.Focus(); selectedListView.Refresh(); }
parvez khanPosted Oct 20, 2007, 11:17 AM
sir i am new in c# programming i want help or any idea , i thik u will give ne my self parvez i make new software for retails medical store in this we are 5 add the medician name in lisatview & but cannot save this 5 item at time in shot . can u help me thanks
Jeff AndrewsPosted Sep 6, 2007, 2:16 PM
Maybe I'm missing something, but this seems very odd. Why do you have 2 different list views (listview_urls and listView_Scheduler)? And you go to the trouble to create the temporary "tmp" ListViewItem and add subitems to it but then you never do anything with it? Perhaps there's some code missing that you intended to be here?
Anshu BhatiaPosted Jun 28, 2007, 4:22 PM
What is listView_Scheduler in the code? Please help!!!
Anshu BhatiaPosted Jun 28, 2007, 4:17 PM
What is listView_Scheduler in the code? Please help!!!
Anshu BhatiaPosted Jun 28, 2007, 4:10 PM
What is listView_Scheduler in the code? Please help!!!
TweisteinPosted Jun 14, 2007, 10:24 AM
this is awesome, man! you should get the Nobel for this finding! the message box rocks! peace!