第一个ListView:
第二个ListView:
第三个ListView:
定义一个KTVUtil类,在里面定义一个静态变量保存歌手图片
//定义一个静态变量保存歌手图片路径
public static string imagePath = "";
在主窗体load事件拿歌手图片的前半部分路径:
//连接字符串
String str = "Data Source=.;Initial Catalog=MyKTV;uid=sa;";
//创建连接对象
SqlConnection con = new SqlConnection(str);
con.Open();
//准备sql语句
string sql = "select resource_path from resource_path where resource_id=1";
//创建命令对象
SqlCommand cmd = new SqlCommand(sql, con);
KTVUtil.imagePath = cmd.ExecuteScalar().ToString();
con.Close();
在第一个ListView_Click事件:
listView2.Items.Clear();
if (listView1.SelectedItems[] != null)
{
//隐藏歌手地区类的listView,显示歌手类的listView
listView1.Visible = false;
listView2.Location = listView1.Location;
listView2.Dock = DockStyle.Fill;
listView2.Visible = true;
this.singer_sex = Convert.ToString(listView1.SelectedItems[].Tag); }
//连接字符串
String str = "Data Source=.;Initial Catalog=MyKTV;uid=sa;";
//创建连接对象
SqlConnection con = new SqlConnection(str);
//准备sql语句
string sql = "select singertype_id,singertype_name from singer_type";
//创建命令对象
SqlCommand cmd = new SqlCommand(sql,con);
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
listView3.Items.Clear(); if(dr.HasRows){
int index = ;
while(dr.Read()){ //每Read一次拿到一个地区
//从数据库中读取一个歌手分类信息,就要创建一个ListViewItem
ListViewItem lvitem = new ListViewItem();
int typeId = Convert.ToInt32(dr["singertype_id"]);
string typename = Convert.ToString(dr["singertype_name"]);
lvitem.Text = typename;
lvitem.Tag = typeId;
lvitem.ImageIndex = index;
listView2.Items.Add(lvitem);
index++; }
}
}
catch
{
MessageBox.Show("系统维护中");
}
finally { }
在第二个ListView_Click事件:
listView1.Visible = false;
listView2.Visible = false;
listView3.Visible = true;
listView3.Dock = DockStyle.Fill;
listView3.Location = listView1.Location;
this.singertype_id = Convert.ToInt32(listView2.SelectedItems[].Tag); string str = "Data source = .; Initial catalog=MyKTV ;Uid = sa;";
SqlConnection con = new SqlConnection(str); string sql = "select singer_id , singer_name , singer_photo_url from singer_info where singer_sex='"+singer_sex+"' and singertype_id="+singertype_id+ "";
SqlCommand com = new SqlCommand(sql, con);
con.Open(); SqlDataReader dr = com.ExecuteReader(); listView3.Items.Clear();
int Imageindex = ; //歌手图像的索引
if (dr.HasRows) {
while (dr.Read())
{
ListViewItem item = new ListViewItem();
string fullpath = KTVUtil.imagePath + "\\" + Convert.ToString(dr["singer_photo_url"]);
imageList3.Images.Add(Image.FromFile(fullpath));
item.Text =Convert.ToString(dr["singer_name"]);
item.Tag = Convert.ToString(dr["singer_id"]);
item.ImageIndex = Imageindex;
listView3.Items.Add(item);
Imageindex++;
}
dr.Close();
}
con.Close();