none
check if dupplicate item in column of listview RRS feed

Answers

  • Well, just search in the items and their subitems. E.g.

    namespace WindowsFormsCS
    {
        using System;
        using System.Data;
        using System.Linq;
        using System.Windows.Forms;
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnSearchItems_Click(object sender, EventArgs e)
            {
                string searchFor = "12";
                int count = lstItems.Items.Cast<ListViewItem>()
                    .Select(i => i.SubItems[2].Text)
                    .Where(t => t.Equals(searchFor))
                    .Count();
                if (count > 0)
                {
                    MessageBox.Show("Item exists.");
                }
            }
        }
    }
    

    Sunday, November 4, 2018 10:40 PM

All replies

  • Well, just search in the items and their subitems. E.g.

    namespace WindowsFormsCS
    {
        using System;
        using System.Data;
        using System.Linq;
        using System.Windows.Forms;
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnSearchItems_Click(object sender, EventArgs e)
            {
                string searchFor = "12";
                int count = lstItems.Items.Cast<ListViewItem>()
                    .Select(i => i.SubItems[2].Text)
                    .Where(t => t.Equals(searchFor))
                    .Count();
                if (count > 0)
                {
                    MessageBox.Show("Item exists.");
                }
            }
        }
    }
    

    Sunday, November 4, 2018 10:40 PM
  • Hello

    I tried your code

    private void btnSearchItems_Click(object sender, EventArgs e)
            {
                string searchFor = "12";
                int count = ListView1.Items.Cast<ListViewItem>()
                    .Select(i => i.SubItems[2].Text)
                    .Where(t => t.Equals(searchFor))
                    .Count();
                if (count > 0)
                {
                    MessageBox.Show("Item exists.");
                }
            }

    But I am getting Error    1    'System.Windows.Forms.ListView.ListViewItemCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>

    And Please help me to create as a function that accept a string parameter and returns true if item exist in the listview

    Monday, November 5, 2018 10:04 AM
  • Then you missed one or more of the imports. Especially for Linq.
    Monday, November 5, 2018 3:12 PM
  • thank you
    Monday, November 5, 2018 7:25 PM