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

Christophe1245

New member
Joined
Nov 6, 2010
Messages
2
Programming Experience
1-3
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

VB.NET:
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!

VB.NET:
lvwColumn.Width = -2

Can someone help me with this?

Thanks in advance en sorry for my bad english.

Best regards,

Christophe
 

Christophe1245

New member
Joined
Nov 6, 2010
Messages
2
Programming Experience
1-3
Solution

VB.NET:
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
 
Top Bottom