Link to home
Start Free TrialLog in
Avatar of Dabas
DabasFlag for Australia

asked on

Column Headers for ListView in VB.NET

I am in the transition stage of going from VB6 mentality to VB.NET mentality, and probably am still doing things the old "wrong" way.

Why is the following code not working for me?
oPrintOrders is a DataSet, and PrintOrders is its adapter. I do not think the problem is dataset related.

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim dc As DataColumn, ch As ColumnHeader

          oPrintOrders.Clear()
          PrintOrders.Fill(oPrintOrders)
          lvJobs.Columns.Clear()'This works OK if I leave the two columns I added at design time!
          For Each dc In oPrintOrders.Tables(0).Columns
               'This also did not work
               'ch = lvJobs.Columns.Add(dc.Caption.ToString, -2, HorizontalAlignment.Left)

               ch = New ColumnHeader()
               ch.Text = dc.Caption.ToString
               ch.Width = -1
               ch.TextAlign = HorizontalAlignment.Left
               lvJobs.Columns.Add(ch)

          Next

          Dim li As ListViewItem
          'this works OK if I manually add two columns at design time and do not clear above.
          For Each oDR In oPrintOrders.Tables(0).Rows()
               li = lvJobs.Items.Add(oDR.Item("JobNumber"))
               li.SubItems.Add(oDR.Item("DeliveryCode"))
          Next
     End Sub

After clicking on the button, I do not get any column headers at all, although from the vertical scroll bar I can see that the listview has been filled.

I do have
          Me.lvJobs.View = System.Windows.Forms.View.Details
Avatar of KBerger
KBerger

Hi Dabas!

Answering to your call for help! ;-)

I'm not quite sure, as I did not test your code yet, but did you try to set the width for the columns you create to something greater zero?
How about 200 for a try?

I'll test your code and send you another hint if the one above doesn't do the job. ;-)

Regards

Kristof
ASKER CERTIFIED SOLUTION
Avatar of KBerger
KBerger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And another one!

Now I know what you wnated to do: You wanted to adjust the sizes of your columns.
Well, to do this, you have to set the width of the items in the columns collection to -1 AFTER you created the items in the list!

Like This:

Me.lvTest.Columns.Add("Test",300,HorizontalAlignment.Left)
Me.lvTest.Columns.Add("Test2",300,HorizontalAlignment.Left)

Dim oItem As ListViewItem
oItem = Me.lvTest.Items.Add("Test")
oItem.SubItems.Add("test2")

Me.lvTest.Columns(0).Width=-2
Me.lvTest.Columns(1).Width=-2

Phew! This was a hard one! ;-)
change ch.width=-1 to ch.width=200(or something)
oDR is not defined

make sure view property of listview is set to details

rest is fine and should work

 
Avatar of Dabas

ASKER

Thanks Kristoff.

Adding the following to the bottom of the procedure had the desired effect.

          For Each ch In lvJobs.Columns
               ch.Width = -1
          Next

Now I understand why in their examples, they first fill the listview, then the columns (with a -1 or -2 value).

As mentioned at the very beginning, I have to free myself from VB6 mentality and get into the .NET one. In VB6 you HAVE to set the columns before you can add subitems....

Thanks again!