ColumnHeader h1 = new ColumnHeader();//创建一个列
            h1.Text = "姓名";            ColumnHeader h2 = new ColumnHeader();//创建第二个列
            h2.Text = "学号";
            listView1.Columns.AddRange(new ColumnHeader[]{h1,h2});//将列添加到空间里
            listView1.View = View.Details;//列的显示模式
            ListViewItem l = new ListViewItem(new string[]{"张三","14"});//实例化一个元素
            listView1.Items.Add(l);//将实例化的数据添加到 控件里
启动时,为啥显示的是3列呢第一列是姓名,第二列是学好,怎么还有个第三列空的。只需要两列的,谢谢大家

解决方案 »

  1.   


    首先初始化listview控件:
    private void SetListView()
            {
                // Set the view to show details.
                listView1.View = View.Details;
                // Allow the user to edit item text.
                listView1.LabelEdit = false;
                // Allow the user to rearrange columns.
                listView1.AllowColumnReorder = false;
                // Select the item and subitems when selection is made.
                listView1.FullRowSelect = true;
                // Display grid lines.
                listView1.GridLines = true;
                // Sort the items in the list in descending order.
                listView1.Sorting = SortOrder.None;
                 
                // Create columns for the items and subitems.
                listView1.Columns.Add("roomTem", "环境温度(℃)");
                listView1.Columns.Add("DC", "直流跳闸时间(s)");
                listView1.Columns.Add("exchange", "交流跳闸时间(s)");
     
                int width = listView1.Width / 3;
                 
                listView1.Columns["roomTem"].Width = width;
                listView1.Columns["DC"].Width = width;
                listView1.Columns["exchange"].Width = width;
     
                ImageList imgList = new ImageList();
                imgList.ImageSize = new Size(1, 24);//分别是宽和高
                listView1.SmallImageList = imgList;   //这里设置listView的SmallImageList ,用imgList撑大
            }
     
     
    其次:为了listview控件能够在随着大小改变自动调整列宽,添加resize事件:
     private void listView1_Resize(object sender, EventArgs e)
            {
                int width = listView1.Width / 3;
     
                listView1.Columns["roomTem"].Width = width;
                listView1.Columns["DC"].Width = width;
                listView1.Columns["exchange"].Width = width;
            }
     
     
    另外:禁止用户拖动列宽,添加ColumnWidthChanging事件:
     private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
            {
                ColumnHeader header = this.listView1.Columns[e.ColumnIndex];
                e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
                e.Cancel = true;
            }       这也是我前几天刚搞出来的,分享给你。
    见:http://my.csdn.net/my/code/detail/44176
      

  2.   

    ListView中,就想把它用再asp.net时候一样,有那么多一行出来,那行应该是不算的...