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
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace RestrictOneListViewtoAnother
- {
- public partial class Form1: Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnForward_Click(object sender, EventArgs e)
- {
- foreach(ListViewItem item in listView1.SelectedItems)
- {
- if (item.Text != "Andy")
- {
- listView1.Items.Remove(item);
- listView2.Items.Add(item);
- }
- }
- }
- private void btnBackward_Click(object sender, EventArgs e)
- {
- foreach(ListViewItem item in listView2.SelectedItems)
- {
- listView2.Items.Remove(item);
- listView1.Items.Add(item);
- }
- }
- SchoolManagementEntities objSchoolManagementEntities = new SchoolManagementEntities();
- private void Form1_Load(object sender, EventArgs e)
- {
- var query = from r in objSchoolManagementEntities.Students select r;
- foreach(var r in query)
- {
- listView1.Items.Add(r.FirstName);
- }
- }
- }
- }
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.