4

I am not getting column Header in listView. only one item(0) is displaying not the sub Item. here is my code. tell me what is wrong in it. Thank you in advance.

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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
desabhotla
  • 51
  • 1
  • 1
  • 4
  • 1
    It's pointless to set objects equal to `Nothing`. And while you do need to call the `Dispose` method on objects that implement `IDisposable`, it's better to wrap their creation in a `Using` statement. There's an awful lot of "noise" in this code. – Cody Gray - on strike Feb 02 '12 at 06:18
  • possible duplicate of [ListView Headers Don't Show Up](http://stackoverflow.com/questions/1936798/listview-headers-dont-show-up) – nawfal Sep 01 '13 at 20:37

2 Answers2

12

You need to change your ListView control's View property to Details in order to see the columns and subitems:

With lvwPatTests
  .View = View.Details

  .CheckBoxes = True
  .GridLines = True
  .FullRowSelect = True
  .HideSelection = False
  .MultiSelect = False
  .Columns.Add("Test")
  .Columns.Add("Price")
End With
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

You already have the objects. Just pass them in:

   With lvwPatTests
        .CheckBoxes = True
        .GridLines = True
        .FullRowSelect = True
        .HideSelection = False
        .MultiSelect = False
        .Columns.Add(header1)
        .Columns.Add(header2)
    End With
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
DRC
  • 11
  • 1