ListView控件中的图标问题
程序目标:
先从数据库中读取员工信息,将其填充到ListView控件中供人流览,根据员工性别字段的不同,男性一种图标,女性一种图标,在同一个ListView中显示
主要代码如下:
DataTable tbl = TransData.GetDataTable( "Select em_id,em_name,em_sex,em_group,em_mobile from Employees order by em_id ");
ltvEmployee.Items.Clear();
for (int i = 0; i < tbl.Rows.Count; i++)
{
ltvEmployee.Items.Add(tbl.Rows[i][ "em_id "].ToString());
ltvEmployee.Items[i].SubItems.Add(tbl.Rows[i][ "em_name "].ToString());
ltvEmployee.Items[i].SubItems.Add(tbl.Rows[i][ "em_sex "].ToString());
ltvEmployee.Items[i].SubItems.Add(tbl.Rows[i][ "em_group "].ToString());
ltvEmployee.Items[i].SubItems.Add(tbl.Rows[i][ "em_mobile "].ToString());
if (ltvEmployee.Items[i].SubItems[2].Text == "男 ") ltvEmployee.Items[i].ImageIndex = 2;
else ltvEmployee.Items[i].ImageIndex = 3;
}
问题:将数据填充到ListView控件后,始终只显示一种图标,即女性的那种图标(ImageIndex为3),不知道怎样解决这个问题,求高手指教!谢谢!
------解决方案--------------------foreach(DataRow dr in tbl.Rows)
{
ListViewItem item = new ListViewItem();
item.text = dr[ "em_id "].ToString();
item.SubItems.Add(dr[ "em_sex "].ToString());
...
if(dr[ "em_sex "].ToString() == "男 ")
{
item.ImageIndex = 2;
}
else
{
item.ImageIndex = 3;
}
ltvEmployee.Items.Add(item);
}
还有确认一下你的ltvEmployee.ImageList 是否绑定了ImageList
------解决方案--------------------if (ltvEmployee.Items[i].SubItems[2].Text == "男 ") ltvEmployee.Items[i].ImageIndex = 2;
else ltvEmployee.Ite