分享

C# ListView控件的使用方法 简介

 偷心无痕 2013-11-21
  1.   

ListView控件:

 (一)  生成一个ListView对象 listView1:

  1. ListView listView1 = new ListView();  

 (二) listView1对象的属性

  1.        //设置listView1的大小  
  2. listView1.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200));  
  3.   
  4. //展示ListView的所有信息  
  5. listView1.View = View.Details;  
  6.              
  7. // 是否运行用户编辑  
  8. listView1.LabelEdit = true;  
  9.  // 是否运行用户调整列的顺序  
  10. listView1.AllowColumnReorder = true;  
  11.         
  12. // .这条语句在每一行的行头加个选择属性。  
  13. listView1.CheckBoxes = true;  
  14.        //要选就全行选中  
  15.        listView1.FullRowSelect = true;  
  16.          
  17. // 展示分割线  
  18.        listView1.GridLines = true;  
  19.          
  20. // 对每一项进行排序  
  21.        listView1.Sorting = SortOrder.Ascending;  
  22.          
  23. //需要的时候显示滚动条  
  24.        listView1.Scrollable = true;  
  25.   
  26. //listView1的列的头部是否可点击,响应事件  
  27.        listView1.HeaderStyle = ColumnHeaderStyle.Clickable;  


 (三)  通过数据库给listview1添加项

  1. /*** 清空listView ***/  
  2. listView1.Clear();  
  3.   
  4. // Create columns for the items and subitems.  
  5. // -2 表示自动调节大小.  
  6. listView1.Columns.Add("序号", -2, HorizontalAlignment.Left);  
  7. listView1.Columns.Add("名称", -2, HorizontalAlignment.Center);  
  8. listView1.Columns.Add("人数", -2, HorizontalAlignment.Center);  
  9.   
  10. /***连接数据库,显示信息***/  
  11. MySQLConnection conn = null;  
  12.   
  13. //第一个参数:数据库名,第二个参数:数据库的用户名,第三个参数:密码  
  14. //这里,group是我连接的数据库,root是数据库的用户名,第三个参数是密码,也为"root"  
  15. conn = new MySQLConnection(new MySQLConnectionString("group""root""root").AsString);  
  16.   
  17. /**group_people是数据库下面的一个表**/  
  18. MySQLCommand cmd = new MySQLCommand("select * from group_people", conn);  
  19.   
  20. //打开连接  
  21. conn.Open();  
  22.   
  23. /**汉字编码问题。执行一遍之后,读出的汉字不乱码**/  
  24. MySQLCommand commn = new MySQLCommand("set names gb2312", conn);  
  25. commn.ExecuteNonQuery();  
  26.   
  27. /**执行cmd语句,即查询语句**/  
  28. MySQLDataReader reader = cmd.ExecuteReaderEx();  
  29.   
  30. int i = 0;  
  31. while (reader.Read())  
  32. {  
  33.     i++;  
  34.     ListViewItem item = new ListViewItem();  
  35.       
  36.     item.SubItems.Clear();  
  37.   
  38.     /**名称**/  
  39.     item.SubItems[0].Text = i.ToString();  
  40.     item.SubItems.Add(reader.GetString(0));  
  41.   
  42.     /**人数**/  
  43.     string tempPeople = reader.GetString(1);  
  44.     string []tempArrayPeople = tempPeople.Split(' ');  
  45.     item.SubItems.Add(tempArrayPeople.Length.ToString());  
  46.   
  47.     listView1.Items.Add(item);  
  48. }  
  49.   
  50. conn.Close();  
完。



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多