windows Forms下listView的使用(转载)
C#如何获取listview选中项
悬赏分:30 - 解决时间:2009-10-3 01:06
RT,Listview中有9项,任意点击一项请问要如何获取,尽量讲得具体些吧~谢谢啦~
最佳答案
选中项的文本值:this.listView1.SelectedItems[0].Text; 选中项的子项的文本值 this.listView1.SelectedItems[0].SubItems[0].Text;(SubItems[0]表示选中项的第一个子项,第二个子项就是SubItem[1],第三个就是SubItems[2]..
..以此类推)
ListView用法示例
代码
this.listView1.View = View.Details;
ListView listView1 = new ListView();
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes. 是否显示复选框
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made. 是否选中整行
listView1.FullRowSelect = true;
// Display grid lines. 是否显示网格
listView1.GridLines = true;
// Sort the items in the list in ascending order. 升序还是降序
listView1.Sorting = SortOrder.Ascending;
this.listView1.Columns.Add("编号", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("姓名", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("年龄", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("电话", 60, HorizontalAlignment.Right);
this.listView1.Visible = true;
for (int i = 20; i >= 1; i--)
{
ListViewItem li = new ListViewItem();
li.SubItems[0].Text = i.ToString();
li.SubItems.Add("aaa");
li.SubItems.Add("25");
li.SubItems.Add("11223344");
this.listView1.Items.Add(li);
}