c#中listview控件添加信息例程

折腾了打扮下午,才搞明白了listview的所以然,记得在大学的时候给外教做过一个项目中,用到了listview这个控件,但是那是在vb中,和C#的使用还是稍微有点差别,书上介绍的也不多,网上写的也乱七八糟。

下面就通过一个小示例解释一下:

加入想在listview中加入以下两条信息。

通过设计模式添加很简单,这里就不说了,我们通过代码来完成。

//设置listview控件
            //MessageBox.Show(listView1.Columns.Count.ToString ());//用于说明一共有多少列
            listView1.View = View.Details;// Set the view to show details.有的时候,如果不设置这个,那么即使你再设计模式下添加了列,运行的时候也不会显示,这里我们用命令控制他显示,当然,我们也可以设计模式下的view属性选择Details.

            listView1.LabelEdit = true;// Allow the user to edit item text.允许用户修改值。
            //listView1.AllowColumnReorder = true;//Allow the user to rearrange columns.
            // Display grid lines.
            listView1.GridLines = true;//显示网格线
            // Sort the items in the list in ascending order.
           // listView1.Sorting = SortOrder.Ascending;

            // Create columns for the items and subitems.

//一共三行            listView1.Columns.Add("name");
            listView1.Columns.Add("age");
            listView1.Columns.Add("rollno");
            ListViewItem firstrecord = new ListViewItem("hope");//这个是第一行第一列
            firstrecord.SubItems.Add("22");//第一行第二列
            firstrecord.SubItems.Add("1001");//第一行第三列
            ListViewItem secondrecord = new ListViewItem("basil");//这个是第二行第一列
            secondrecord.SubItems.Add("23");//第二行第二列
            secondrecord.SubItems.Add("1002");//第二行第三列
            listView1.Items.Add(firstrecord);//把第一行添加上
            listView1.Items.Add(secondrecord);//把第二行添加上

 

posted @ 2010-02-15 11:17  猪悟能  阅读(1024)  评论(0编辑  收藏  举报