C#第三章

一.ImageList:存储图像集合

Images 存储的所有图像
ImageSize 图像的大小
ColorDepth 颜色数
TransparentColor 被视为透明的颜色
先设置ColorDepth、ImageSize属性值再添加图片,反之不能更改这两个属性值

二.ListView:存储项集合

Items ListView中的项
View 指定那种视图显示
LargeImageList 大图标图像的ImageList控件
SmallImageList 小图标图像的ImageList控件


三.ContextMenuStrip

Items 快捷菜单项的集合
DisplayStyle 每一项显示的状态(文字,图像)

在控件上选择ContextMenuStrip属性绑定快捷菜单

四.动态绑定ListView中的数据

#region 动态绑定ListView中的数据

//定位到父项
ListViewItem itemc = new ListViewItem("C盘:",0);
//第一种:通过父项.SubItems.Add()添加单个子项
itemc.SubItems.Add("本地磁盘");
itemc.SubItems.Add("250GB");
itemc.SubItems.Add("1KB");

ListViewItem itemd = new ListViewItem("D盘:",1);
//方式二:通过父项.SubItems.AddRange()添加多个子项
itemd.SubItems.AddRange(new string[]{"本地磁盘","1TB","250GB"});

扫描二维码关注公众号,回复: 2594097 查看本文章


//最后一步:将父项以及父项的子项集合添加到ListView当中
this.lvwindows.Items.Add(itemc);
this.lvwindows.Items.Add(itemd);

//通过下标定位到父项然后添加子项列表数据
ListViewItem iteme = this.lvwindows.Items[2];
iteme.SubItems.AddRange(new string[] { "本地磁盘", "1TB", "250GB" });

#endregion

五.动态从数据库获取数据绑定
string constr = "Data Source=.;Initial Catalog=SchoolDB;User ID=sa;Password=.";
SqlConnection con = new SqlConnection(constr);
try
{
con.Open();
string sql = @"select Grade.*,Student.* from Grade,Student where Grade.GradeId=Student.GradeId
and Student.StudentName like '%"+this.txtName.Text+"%' ";

SqlCommand com = new SqlCommand(sql,con);
SqlDataReader reader=com.ExecuteReader();
//判断读取出来的数据为不为空
if (reader.HasRows) {
while(reader.Read()){
ListViewItem item = new ListViewItem(reader["StudentNo"].ToString());
item.SubItems.AddRange(new string[] { reader["StudentName"].ToString(), reader["Sex"].ToString(), reader["GradeName"].ToString() });
item.Tag = (int)reader["StudentNo"];
this.lvStudentList.Items.Add(item);
}
}

}
catch (Exception x)
{
MessageBox.Show(x.ToString());
}
finally {
con.Close();
}

获取选中项的Tag值:this.lvStudentList.SelectedItems[0].Tag.ToString()

猜你喜欢

转载自www.cnblogs.com/ws1149939228/p/9434922.html