//设置大图标视图方式对应的imagelist
listView1.LargeImageList = imageList2;
//设置小图标视图方式对应的imagelist
listView1.SmallImageList = imageList1;
listView1.Items.Clear();
listView1.Update();
listView1.Items.Add("张三", 0);
listView1.Items[0].SubItems.Add("男");
listView1.Items[0].SubItems.Add("90");
listView1.Items[0].SubItems.Add(" 湖南");
listView1.Items[0].ImageIndex = 4; //对应在imagelist中图片索引
listView1.Items.Add("张三1", 1);
listView1.Items[1].SubItems.Add("男1");
listView1.Items[1].SubItems.Add("901");
listView1.Items[0].ImageIndex = 5; //对应在imagelist中图片索引
listView1.Items[1].SubItems.Add(" 湖南1");
listView1.Items.Add("张三2", 2);
listView1.Items[2].SubItems.Add("男2");
listView1.Items[2].SubItems.Add("902");
listView1.Items[0].ImageIndex = 6; //对应在imagelist中图片索引
listView1.Items[2].SubItems.Add(" 湖南2");
listView1.EndUpdate();
//item颜色变化
for (int i = 0; i < this.listView1.Items.Count; i++)
{
//if (listView1.SelectedItems.Count > 0)
//{
// listView1.ForeColor = Color.White;
//}
listView1.Items[i].ForeColor = Color.Red;
}
//选中行获取值和改变选中颜色
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
MessageBox.Show(listView1.SelectedItems[0].Text); //获得选择项的第一列的值
// MessageBox.Show(listView1.SelectedItems[0].Tag.ToString());//获得选择项的tag值
listView1.SelectedItems[0].ForeColor = Color.Blue; ;
MessageBox.Show(listView1.SelectedItems[0].SubItems[1].Text);//获得选择项的第二列的值
}
else
{
MessageBox.Show("请选择后再单击");
}
}