Introduction
In this article we will see how to disable and grey out any specific listviewitem based on condition.
Step 1: Create windows forms application
Form.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 ListView_DisableSpecificItem
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- SchoolManagementEntities objSchoolManagementEntities = new SchoolManagementEntities();
- private void Form1_Load(object sender, EventArgs e)
- {
- var query = from r in objSchoolManagementEntities.Students select r;
-
-
-
- foreach (var p in query)
- {
- ListViewItem item = new ListViewItem();
-
- item.Text = p.FirstName;
-
- if (item.Text == "Andy")
- {
- item.BackColor = System.Drawing.Color.Gray;
- }
-
- listView1.Items.Add(item);
- }
- }
-
- private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
- {
- if (e.IsSelected && e.Item.Text == "Andy")
- {
- e.Item.Selected = false;
- }
- }
- }
- }
Output of the application looks like this
Summary
In this blog we have seen how we can disable any specific ListViewItem based on condition. Happy coding!