2012-08-10 65 views
1

我正在製作很多列的列表視圖,並且我想讓列表在所有子項目中可點擊,如下圖所示。clickable listview for all subItem in its item c#

enter image description here

不過,我得到的是像下面的圖片。

enter image description here

這是我的代碼:

private void button6_Click(object sender, EventArgs e) 
     { 
      ListViewItem listviewitem; 

      listviewitem = new ListViewItem("John"); 
      listviewitem.SubItems.Add("Smith"); 
      listviewitem.SubItems.Add("kaya"); 
      listviewitem.SubItems.Add("bun"); 
      this.listView1.Items.Add(listviewitem); 
      this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick); 
//show header 
      listView1.View = View.Details; 

      // Loop through and size each column header to fit the column header text. 
      foreach (ColumnHeader ch in this.listView1.Columns) 
      { 
       ch.Width = -2; 
      } 
} 

這是我columnclick事件處理程序。

// ColumnClick event handler. 
    private void ColumnClick(object o, ColumnClickEventArgs e) 
    { 
     // Set the ListViewItemSorter property to a new ListViewItemComparer 
     // object. Setting this property immediately sorts the 
     // ListView using the ListViewItemComparer object. 
     this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column); 
    } 




    } 
class ListViewItemComparer : IComparer 
{ 
    private int col; 
    public ListViewItemComparer() 
    { 
     col = 0; 
    } 
    public ListViewItemComparer(int column) 
    { 
     col = column; 
    } 
    public int Compare(object x, object y) 
    { 
     return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); 
    } 
} 
+0

哪裏是第一張圖片的?它看起來不像是一個winform。你是否想要在Winform中獲得WPF功能? – GrayFox374 2012-08-10 02:22:03

+1

假設您指的是就地編輯,您應該使用第三方控件,如[DevExpress](http://devexpress.com)或[Telerik](http://www.telerik.com)提供的第三方控件)。 – Bernard 2012-08-10 02:23:29

+0

你可能想看看telerik – 2012-08-10 03:54:53

回答

5
listView1.FullRowSelect = true; 
+1

+1。很容易忽略這個屬性! – 2012-08-10 02:35:23