2012-02-02 55 views
2

我沒有在列表視圖中獲得列標題。只有一個項目(0)不顯示子項目。這是我的代碼。告訴我它有什麼問題。 預先感謝您。列表視圖列標題不顯示VB.Net

Dim PTCode As Integer = CInt(ChildPatnameTag) 
ClearSQl() 

    CheckState() 
    strSql = "select tCode,tprice from patTests where pCode=" & PTCode 
    strConn.Open() 
    Dim TCmdSelect As New OleDbCommand(strSql, strConn) 
    Dim TReader As OleDbDataReader = TCmdSelect.ExecuteReader() 
    'Column Header 
    Dim header1, header2 As ColumnHeader 
    header1 = New ColumnHeader 
    header1.TextAlign = HorizontalAlignment.Left 
    header1.Text = "Test" 
    header1.Width = 250 
    header2 = New ColumnHeader 
    header2.TextAlign = HorizontalAlignment.Left 
    header2.Text = "Price" 
    header2.Width = 50 
    With lvwPatTests 
     .CheckBoxes = True 
     .GridLines = True 
     .FullRowSelect = True 
     .HideSelection = False 
     .MultiSelect = False 
     .Columns.Add("Test") 
     .Columns.Add("Price") 
    End With 
    If TReader.HasRows Then 
     Do While TReader.Read 
      ClearSQl() 
      strSql = "Select tName from tests where tCode=" & TReader("tCode") 
      Dim TCCmdSelect As New OleDbCommand(strSql, strConn) 
      Dim TCReader As OleDbDataReader = TCCmdSelect.ExecuteReader() 
      If TCReader.HasRows Then 
       Do While TCReader.Read 
         For i = 0 To TCReader.FieldCount - 1 
         ' Create List View Item (Row) 
         Dim lvi As New ListViewItem 
         ' First Column can be the listview item's Text 
         lvi.Text = TCReader.Item("tName").ToString 
         ' Second Column is the first sub item 
         lvi.SubItems.Add(TReader.Item("tprice")) 
         MsgBox(TCReader.Item("tName").ToString) 
         MsgBox(TReader.Item("tprice")) 
         ' Add the ListViewItem to the ListView 
         lvwPatTests.Items.Add(lvi) 
        Next 
       Loop 
       TCCmdSelect.Dispose() 
       TCReader.Close() 
       TCReader = Nothing 
      Else 
       TCCmdSelect.Dispose() 
       TCReader.Close() 
       TCReader = Nothing 
      End If 
     Loop 
     TReader.Close() 
     TReader = Nothing 
    End If 
+1

將對象設置爲Nothing的意義毫無意義。雖然你需要在實現IDisposable的對象上調用Dispose方法,但最好將它們的創建封裝在Using語句中。這段代碼中有很多「噪音」。 – 2012-02-02 06:18:24

+0

可能重複的[ListView標題不顯示](http://stackoverflow.com/questions/1936798/listview-headers-dont-show-up) – nawfal 2013-09-01 20:37:39

回答

8

您需要才能看到列和子項的ListView控件的視圖屬性更改爲Details

With lvwPatTests 
    .View = View.Details 

    .CheckBoxes = True 
    .GridLines = True 
    .FullRowSelect = True 
    .HideSelection = False 
    .MultiSelect = False 
    .Columns.Add("Test") 
    .Columns.Add("Price") 
End With 
+0

感謝您的幫助Lars Tech。 – desabhotla 2012-02-03 07:17:03

1

你已經擁有的對象。只需將它們傳入:

With lvwPatTests 
     .CheckBoxes = True 
     .GridLines = True 
     .FullRowSelect = True 
     .HideSelection = False 
     .MultiSelect = False 
     .Columns.Add(header1) 
     .Columns.Add(header2) 
    End With