Introduction

In this blog we will see how we can restrict ListViewItem movement from one ListView to another based on condition.

Step 1: Create windows forms application


Form1.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace RestrictOneListViewtoAnother
  11. {
  12. public partial class Form1: Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void btnForward_Click(object sender, EventArgs e)
  19. {
  20. foreach(ListViewItem item in listView1.SelectedItems)
  21. {
  22. if (item.Text != "Andy")
  23. {
  24. listView1.Items.Remove(item);
  25. listView2.Items.Add(item);
  26. }
  27. }
  28. }
  29. private void btnBackward_Click(object sender, EventArgs e)
  30. {
  31. foreach(ListViewItem item in listView2.SelectedItems)
  32. {
  33. listView2.Items.Remove(item);
  34. listView1.Items.Add(item);
  35. }
  36. }
  37. SchoolManagementEntities objSchoolManagementEntities = new SchoolManagementEntities();
  38. private void Form1_Load(object sender, EventArgs e)
  39. {
  40. var query = from r in objSchoolManagementEntities.Students select r;
  41. foreach(var r in query)
  42. {
  43. listView1.Items.Add(r.FirstName);
  44. }
  45. }
  46. }
  47. }
Output of the application looks like this



Summary

In this blog we have seen how we can restrict ListViewItem movement from one ListView to another based on condition.

Happy coding.