How to search for a subkey in C # Lsitview

Source: Internet
Author: User

---restore content starts---

The ListView control is often used in WinForm development, if there are several ways to query the subitem subkey?

1. Use loop Traversal mode: (2.0)

<summary>///finds the index///</summary>///<param name= "View" > The ListView Control to find for a specific column in the ListView that contains the specified value </param>///<param name= "name" > specified column name</param>///<param name= "val" > Value to find </param>// /<returns> Find the value,-1 is not found </returns>public int getlistviewid (ListView view, string name, String val) {for (int i = 0; I < view. Items.Count; i++) {if (view. Items[i]. Subitems[name]. Text = = val) return i;} return-1;}

2. Using LINQ (3.5+)

LINQ queries can be used in the new version of the NET Framework, which simplifies the amount of code and improves readability.

ListViewItem li = listviewuser.items.cast<listviewitem> (). First (x = X.text = = "1005" && x.subitems["City"]. Text = = "Beijing"); string val = li. subitems["City"]. Text;

Other methods haven't been found yet, make a record, memo

---restore content ends---

How to search for a subkey in C # Lsitview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.