ARTICLE

How to Swap Two Rows of listview in Upward/Downward Direction

Posted by Muhammad Shakir Articles | Windows Forms C# March 29, 2007
This article describe how two data rows of a listview are changed.
Reader Level:

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.

Login to add your contents and source code to this article
post comment
     

this rocks... thank you!!

Posted by rezo greywords Nov 16, 2011

I'm sorry, but this only moved the first subitem from the selected row, not the whole row for me anyway....

Posted by Kevin Davis Aug 30, 2011

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.

Posted by Lennie Kuah Feb 24, 2011

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(); }

Posted by Kjartan Apr 17, 2009

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

Posted by parvez khan Oct 20, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.