如何通过指定项目标签直接获取listview项目文本 [英] How to directly get listview item text by specifying item tag
问题描述
我有一个名为"listview1"的Listview.我正在列表视图中显示学生姓名.
我在数据表中收集了学生ID和姓名.
//在列表视图中附加学生姓名
I have a Listview named "listview1". I am displaying students name in the list view.
I got collection of students Id and name in a data table.
// appending the students name in the listview
foreach (DataRow dr in dtStudentInfo.Rows)
{
Item = listView1.Items.Add(dr["strName"].ToString());
Item.Tag = dr["nId"].ToString();
}
问题:如果我知道学生编号,如何获得学生的姓名?
例如:对于学生ID 5,我想知道学生的名字.
在这里,我可以通过将整个项目集合放入for循环和
中来做到这一点
我可以比较标签.但是我想要一个直接的方法,我可以将标签指定为5并获取
标签的学生姓名?
question : How can i get name of the student if i know the student Id?
Eg: For the student Id 5 i want to know the name of the student.
Here i can do this by getting whole item collection inside a for loop and
i can compare the tag. But i want a direct method where i can specify the tag as 5 and get
student name for the respective tag?
推荐答案
您可以使用LINQ搜索特定文本.您只需要很少的知识就可以做到.
请检查此 [
Hi,
You can search for particular text using LINQ. you need little knowledge to do that.
Please check this[^] discussion.
You will find solution.
Thanks
-Amit Gajjar
For adding items to list view there is overloaded method which takes 3 parameters, with key, name and image index
while retrieving use Find(key) or
listView1.Items.IndexOfKey(key)
listView1.Items.Add("key", "Text", 0);
这篇关于如何通过指定项目标签直接获取listview项目文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!