第一个ListView:
第二个ListView:
第三个ListView:
定义一个KTVUtil类,在里面定义一个静态变量保存歌手图片
1 //定义一个静态变量保存歌手图片路径 2 public static string imagePath = "";
在主窗体load事件拿歌手图片的前半部分路径:
1 //连接字符串 2 String str = "Data Source=.;Initial Catalog=MyKTV;uid=sa;"; 3 //创建连接对象 4 SqlConnection con = new SqlConnection(str); 5 con.Open(); 6 //准备sql语句 7 string sql = "select resource_path from resource_path where resource_id=1"; 8 //创建命令对象 9 SqlCommand cmd = new SqlCommand(sql, con); 10 KTVUtil.imagePath = cmd.ExecuteScalar().ToString(); 11 con.Close();
在第一个ListView_Click事件:
1 listView2.Items.Clear(); 2 if (listView1.SelectedItems[0] != null) 3 { 4 //隐藏歌手地区类的listView,显示歌手类的listView 5 listView1.Visible = false; 6 listView2.Location = listView1.Location; 7 listView2.Dock = DockStyle.Fill; 8 listView2.Visible = true; 9 this.singer_sex = Convert.ToString(listView1.SelectedItems[0].Tag); 10 11 } 12 //连接字符串 13 String str = "Data Source=.;Initial Catalog=MyKTV;uid=sa;"; 14 //创建连接对象 15 SqlConnection con = new SqlConnection(str); 16 //准备sql语句 17 string sql = "select singertype_id,singertype_name from singer_type"; 18 //创建命令对象 19 SqlCommand cmd = new SqlCommand(sql,con); 20 try 21 { 22 con.Open(); 23 SqlDataReader dr = cmd.ExecuteReader(); 24 listView3.Items.Clear(); 25 26 if(dr.HasRows){ 27 int index = 0; 28 while(dr.Read()){ //每Read一次拿到一个地区 29 //从数据库中读取一个歌手分类信息,就要创建一个ListViewItem 30 ListViewItem lvitem = new ListViewItem(); 31 int typeId = Convert.ToInt32(dr["singertype_id"]); 32 string typename = Convert.ToString(dr["singertype_name"]); 33 lvitem.Text = typename; 34 lvitem.Tag = typeId; 35 lvitem.ImageIndex = index; 36 listView2.Items.Add(lvitem); 37 index++; 38 39 } 40 } 41 } 42 catch 43 { 44 MessageBox.Show("系统维护中"); 45 } 46 finally { 47 48 } 49
在第二个ListView_Click事件:
1 listView1.Visible = false; 2 listView2.Visible = false; 3 listView3.Visible = true; 4 listView3.Dock = DockStyle.Fill; 5 listView3.Location = listView1.Location; 6 this.singertype_id = Convert.ToInt32(listView2.SelectedItems[0].Tag); 7 8 string str = "Data source = .; Initial catalog=MyKTV ;Uid = sa;"; 9 SqlConnection con = new SqlConnection(str); 10 11 string sql = "select singer_id , singer_name , singer_photo_url from singer_info where singer_sex='"+singer_sex+"' and singertype_id="+singertype_id+ ""; 12 SqlCommand com = new SqlCommand(sql, con); 13 con.Open(); 14 15 SqlDataReader dr = com.ExecuteReader(); 16 17 listView3.Items.Clear(); 18 int Imageindex = 0; //歌手图像的索引 19 if (dr.HasRows) { 20 while (dr.Read()) 21 { 22 ListViewItem item = new ListViewItem(); 23 string fullpath = KTVUtil.imagePath + "\" + Convert.ToString(dr["singer_photo_url"]); 24 imageList3.Images.Add(Image.FromFile(fullpath)); 25 item.Text =Convert.ToString(dr["singer_name"]); 26 item.Tag = Convert.ToString(dr["singer_id"]); 27 item.ImageIndex = Imageindex; 28 listView3.Items.Add(item); 29 Imageindex++; 30 } 31 dr.Close(); 32 } 33 con.Close();