SIGN UP MEMBER LOGIN:    
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
share 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
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor