Results 1 to 2 of 2

Thread: Problems adding auto width (.width = -2) to existing listview class

  1. #1
    Christophe1245 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2010
    Posts
    2
    Reputation
    0

    Problems adding auto width (.width = -2) to existing listview class

    Hi Forum,

    I'm just starting with vb.net and I'm facing a problem that would probably be relatively easy to fix for experienced developers like you.

    I'm using the next class to fill my listview

    Code:
    Imports System.Data.SqlClient
    
    Public Class ListViewData
       Public Sub FillListView(ByRef MyListView As ListView, _
                               ByRef myData As SqlDataReader) 
          Dim lvwColumn As ColumnHeader
          Dim itmListItem As ListViewItem
    
          Dim shtCntr As Short
    
          MyListView.Clear()
          For shtCntr = 0 To myData.FieldCount() - 1
             lvwColumn = New ColumnHeader()
             lvwColumn.Text = myData.GetName(shtCntr)
             MyListView.Columns.Add(lvwColumn)
          Next
    
          Do While myData.Read
             itmListItem = New ListViewItem()
             itmListItem.Text = myData(0)
    
             For shtCntr = 1 To myData.FieldCount() - 1
                If myData.IsDBNull(shtCntr) Then
                   itmListItem.SubItems.Add("")
                Else
                   itmListItem.SubItems.Add(myData.GetString(shtCntr))
                End If
             Next shtCntr
    
             MyListView.Items.Add(itmListItem)
          Loop
       End Sub
    End Class
    Now my problem:
    I want all the listview columns to be in auto width. I tried adding the next line of code but then only my first column fills my entering listview width. All other columns are not affected!

    Code:
    lvwColumn.Width = -2
    Can someone help me with this?

    Thanks in advance en sorry for my bad english.

    Best regards,

    Christophe

  2. #2
    Christophe1245 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2010
    Posts
    2
    Reputation
    0

    Solution

    Code:
    For Each c As ColumnHeader In MyListView.Columns
    c.Width = -2
    Next
    I just figured out what I was doing wrong! Width = -2 in my code before looping through the data and filling up my listview! (stupid of me)

    Best regards,

    Christophe

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking