ListView with FindItemBykey

3 posts, 0 answers
  1. kevin
    kevin avatar
    1 posts
    Member since:
    Jun 2011

    Posted 06 Mar 2012 Link to this post


    I want to find item in listview , but don't know how to use the FindItemByKey method, can you give a sample?
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Mar 2012 Link to this post

    Hi Kevin,

    The FindByKey method finds an item by the Key value of the ListViewDataItem. So, for exmaple.

    1: Create a simple User class
    public class User
        public User(int id, string name)
            this.Id = id;
            this.Name = name;
        public int Id
        { get; set; }
        public string Name
        { get; set; }

    2: We can give a list of users as the datasource of the ListView, give each item in the listview a key value and then find by that key value
    // build a datasource of users
    int i = 1;
    List<User> users = new List<User>();
    while (i <= 10)
       users.Add(new User(i, "User " + i.ToString()));
    // assign the datasource, valuemember and displaymember
    this.radListView1.DataSource = users;
    this.radListView1.DisplayMember = "Name";
    this.radListView1.ValueMember = "Id";
    // give each of the data items a key (= the id of the user)
    foreach (ListViewDataItem item in this.radListView1.Items)
        item.Key = item.Value;
    // we can now find the item by the key
    ListViewDataItem dataItem = (ListViewDataItem)this.radListView1.FindItemByKey(2, true);
    User theUser = (User)dataItem.DataBoundItem;
    MessageBox.Show(theUser.Id.ToString() + " " + theUser.Name);

    Hope that helps

  3. Ivan Petrov
    Ivan Petrov avatar
    717 posts

    Posted 09 Mar 2012 Link to this post

    Hi guys,

    The example Richard has provided exactly demonstrates how to use the FindByKey method. Remember to mark his post as an answer if it has helped you, so others can find it if they have the same question.

    If there is anything else you need help with, feel free to write back.

    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top