ListView属性
LargeImageList 大图像列表
SmallIageList 小图像列表
Columns 列的集合
FullRowSelect 整行选择
Items 项的集合
GridLines 是否显示网格
SelectedItems 选中项的集合
MultiSelect 是否允许选择多项
View 视图(五种视图)
LargeIcon 大图标
SmallIcon 小图标
Details 详细信息
List 列表视图
Tile 平铺视图
ImageList 图像列表控件
为窗体上的其他控件提供图片
注意:先设置大小(ImageSize),后添加图像(Images) ,避免图像失真
ContextMenuStrip 控件 (右键菜单)
如果某个控件,需要添加右键菜单 ,需要设置该控件的ContextMenuStrip属性
从数据库中读取数据,显示在ListView中
string conStr=”server=.;database=myschool;uid=sa;pwd=sa”;
SqlConnection connection=new SqlConnection(conStr);
string sql=”select studentno,studentname,address from student”;
SqlCommand cmd=new SqlCommand(sql,connection);
try
{
connection.Open();
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
string studentNo=reader[“studentno”].ToString();
string studentName=reader[“studentname”].ToString();
string address=reader[“address”].ToString();
ListViewItem item=new ListViewItem(studentNo);
item.Tag=studentNo;
item.SubItems.AddRange(new string[]{studentName,address});
lv***.Items.Add(item);
}
reader.Close();
}
catch(Exception)
{
}
finally
{
connection.Close();
}
获取ListView选中项的文本
lv***.SelectedItems[0].Text
获取ListView选中项的子项值
lv***.SelectedItems[0].SubItems[1].Text //第1个子项的文本
获取ListView选中项的Tag值
如果要获取选中项的Tag值,在读取时,必须设置Tag值
int studentNo=Convert.ToInt32(lv***.SelectedItems[0].Tag);