Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 948
  • Last Modified:

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
0
Dabas
Asked:
Dabas
  • 3
1 Solution
 
KBergerCommented:
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
0
 
KBergerCommented:
Hi again!

Well, it was the suspicious "-1"-Setting for the width of your columns.

This one works okay:

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")

I didn't bother about the Dataset-thingies, as the dataset-related code is okay anyway.

Hope that helped you out!

Kristof
0
 
KBergerCommented:
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! ;-)
0
 
manialokCommented:
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

 
0
 
DabasAuthor Commented:
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!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now