1.根據名稱key找到到對應的索引 並且選中指定行並對其設置背景色
string selecttext = myTable.Rows[0][0].ToString().Trim() + " [" + myTable.Rows[0][1].ToString().Trim() + "]";
int selectindex = 0;
foreach (ListViewItem eachItem in this.lvDoor.Items)
{
if (eachItem.Text == selecttext)
{
selectindex = eachItem.Index;
}
}
lvDoor.Items[selectindex].Selected = true;
lvDoor.Items[selectindex].Checked = true;
for (int i = 0; i < lvDoor.Items.Count; i++)
{
if (lvDoor.Items[i].BackColor == Color.RoyalBlue)
{
lvDoor.Items[i].BackColor = Color.Azure;
}
}
lvDoor.Items[selectindex].BackColor = Color.RoyalBlue;
2.填充listview
ListViewItem rdInfo = new ListViewItem("");
rdInfo.SubItems.Add("");
rdInfo.SubItems.Add("");
rdInfo.SubItems.Add(card_no);
rdInfo.SubItems.Add("");
rdInfo.SubItems.Add("");
rdInfo.SubItems.Add("");
rdInfo.SubItems.Add(time);
rdInfo.SubItems.Add("");
this.lvRunInfo.Items.Add(rdInfo);
3.調整listview的寬度和高度
ImageList imageList = new ImageList(); //設置行高20
imageList.ImageSize = new System.Drawing.Size(1, 20); //分別是寬和高
lvDevInfo.SmallImageList = imageList;