ListView Column widths not sizeing correctly


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ListView Column widths not sizeing correctly

  1. #1
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263

    ListView Column widths not sizeing correctly

    First I am new to VB.Net but have been programming vb6 for many years.

    I am adding records from a table in my database to a listview control.
    When I add the Column headers and set the column width to -2, auto-resize.
    The ListViewItem column ends up wider than the width of the control and all of the subitems columns are too small.
    I tried using -1, size to longest item, but then all of the columns are too small.
    I even wait until all the records are added before I create the column headers so there would be data for it to compare to; but it still does not work.
    Code:
      Private Sub LoadListView(ByVal lvwCtl As ListView, ByVal connectionString As String)
        Dim lvwItem As New ListViewItem()
        Dim Sql As String
        Sql = "SELECT LastName, FirstName, Title, Photo FROM dbo.Employees ORDER BY LastName "
    
        ' Set the view to show details.
        lvwCtl.View = View.Details
        ' Allow the user to rearrange columns.
        lvwCtl.AllowColumnReorder = True
        ' Select the item and subitems when selection is made.
        lvwCtl.FullRowSelect = True
        ' Display grid lines.
        lvwCtl.GridLines = True
        ' Sort the items in the list in ascending order.
        lvwCtl.Sorting = Windows.Forms.SortOrder.Ascending 
    
        Using connection As New SqlConnection(connectionString)
          Dim command As New SqlCommand(Sql, connection)
          connection.Open()
          Dim reader As SqlDataReader = command.ExecuteReader()
          Try
            While reader.Read()
              lvwItem = lvwCtl.Items.Add(reader.GetString(0))
              lvwItem.SubItems.Add(reader.GetString(1))
              lvwItem.SubItems.Add(reader.GetString(2))
            End While
            ' SetUp Column Headers
            lvwCtl.Columns.Add(reader.GetName(0), -2, HorizontalAlignment.Left)
            lvwCtl.Columns.Add(reader.GetName(1), -2, HorizontalAlignment.Left)
            lvwCtl.Columns.Add(reader.GetName(2), -2, HorizontalAlignment.Left)
    
          Finally
            ' Always call Close when done reading.
            reader.Close()
          End Try
        End Using
      End Sub

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Ok, I Found It.
    You have to add all of the column headers first.
    Then use the Various AutoResize methods to set the widths.
    What I did was to first set all of them to the content size using AutoResizeColumns() method.
    Next I loop through each, save the current column width, and then resize to the header width using the AutoResizeColumn() method.
    If the saved width is larger, I then reset the column width back to the content width by simply setting the columns width property to the saved width value.
    I could have used the AutoResizeColumn() method again, but I already had the size saved, so I just used it.
    Final result looks good!
    Here is the modified routine:
    Code:
      Private Sub LoadListView(ByVal lvwCtl As ListView, ByVal connectionString As String)
        Dim cw As Integer
        Dim I As Integer
        Dim lvwItem As New ListViewItem()
        Dim Sql As String
        Sql = "SELECT LastName, FirstName, Title, Photo FROM dbo.Employees ORDER BY LastName "
    
        'Set the view to show details.
        lvwCtl.View = View.Details
        'Allow the user to rearrange columns.
        lvwCtl.AllowColumnReorder = True
        'Select the item and subitems when selection is made.
        lvwCtl.FullRowSelect = True
        'Display grid lines.
        lvwCtl.GridLines = True
        'Sort the items in the list in ascending order.
        lvwCtl.Sorting = Windows.Forms.SortOrder.Ascending
    
        Using connection As New SqlConnection(connectionString)
          Dim command As New SqlCommand(Sql, connection)
          connection.Open()
          Dim reader As SqlDataReader = command.ExecuteReader()
          Try
            While reader.Read()
              lvwItem = lvwCtl.Items.Add(reader.GetString(0))
              lvwItem.SubItems.Add(reader.GetString(1))
              lvwItem.SubItems.Add(reader.GetString(2))
              'If member is type integer 
              'lvwItem.SubItems.Add(reader.GetInt32(3).ToString())
              'If member is type Double
              'lvwItem.SubItems.Add(reader.GetDouble(4).ToString())
            End While
            'Add Column Headers 
            For I = 0 To 2
              lvwCtl.Columns.Add(reader.GetName(I))
            Next I
            'Sizing only works after all headers have been added
            'Set all column widths to content size
            lvwCtl.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            'Resize columns to the larger of content or header
            For I = 0 To 2
              cw = lvwCtl.Columns(I).Width
              lvwCtl.AutoResizeColumn(I, ColumnHeaderAutoResizeStyle.HeaderSize)
              If cw > lvwCtl.Columns(I).Width Then lvwCtl.Columns(I).Width = cw
            Next I
          Finally
            ' Always call Close when done reading.
            reader.Close()
          End Try
        End Using
      End Sub

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2007, 10:20 PM
  2. Replies: 1
    Last Post: 09-16-2006, 08:37 PM
  3. Replies: 6
    Last Post: 07-19-2006, 09:03 AM
  4. invoke listview column click in code
    By kevin in forum .NET
    Replies: 0
    Last Post: 10-01-2002, 01:48 PM
  5. Replies: 0
    Last Post: 02-11-2002, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center