This is a migrated thread and some comments may be shown as answers.

ListView with FindItemBykey

2 Answers 224 Views
ListView
This is a migrated thread and some comments may be shown as answers.
kevin
Top achievements
Rank 1
kevin asked on 07 Mar 2012, 03:22 AM
Hi,

I want to find item in listview , but don't know how to use the FindItemByKey method, can you give a sample?
 
thanks

2 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 07 Mar 2012, 12:58 PM
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()));
   i++;
}
// 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
Richard



0
Ivan Petrov
Telerik team
answered on 09 Mar 2012, 05:36 PM
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 >>
Tags
ListView
Asked by
kevin
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Ivan Petrov
Telerik team
Share this question
or