ListView 用法,添加和获取内容

代码

 1         string connString2 = "Data Source=.;Initial Catalog=music;Persist Security Info=True;User ID=sa;Password=sa"
 2 
 3         private void testForm_Load(object sender, EventArgs e)
 4         {
 5             DataSet ds = new DataSet();
 6             ds = Kugou.Data.DBHelper.GetDataSet(connString2, "select songid,songname,singer from song");
 7             listView1.GridLines = true;
 8             listView1.FullRowSelect = true;
 9             listView1.View = View.Details;
10             listView1.Scrollable = true;a
11             listView1.MultiSelect = false;
12             listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable; 
13 
14             listView1.Columns.Add("歌曲ID"60, HorizontalAlignment.Center);
15             listView1.Columns.Add("歌曲名称"200, HorizontalAlignment.Center);
16             listView1.Columns.Add("歌手名称"200, HorizontalAlignment.Center); 
17 
18             listView1.Visible = true
19 
20             foreach (DataRow dr in ds.Tables[0].Rows)
21             {
22                 ListViewItem item = new ListViewItem();
23                 item.SubItems.Clear();
24                 item.SubItems[0].Text = dr["songid"].ToString();
25                 item.SubItems.Add(dr["songname"].ToString());
26                 item.SubItems.Add(dr["singer"].ToString()); 
27 
28                 listView1.Items.Add(item);
29             }
30         } 
31 
32         private void listView1_Click(object sender, EventArgs e)
33         {
34             int index = int.Parse(this.listView1.SelectedIndices[0].ToString()); //获取当前项的序号
35             label1.Text = listView1.Items[index].SubItems[0].Text + "-" + listView1.Items[index].SubItems[1].Text + "-" + listView1.Items[index].SubItems[2].Text; //获取其中内容
36         }
37 

 

 

posted @ 2010-07-14 15:53  Zoya Qiu  阅读(320)  评论(0编辑  收藏  举报