?
Solved

Column Headers for ListView in VB.NET

Posted on 2003-03-24
5
Medium Priority
?
928 Views
Last Modified: 2009-12-16
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
Comment
Question by:Dabas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 3

Expert Comment

by:KBerger
ID: 8200951
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
 
LVL 3

Accepted Solution

by:
KBerger earned 1000 total points
ID: 8200984
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
 
LVL 3

Expert Comment

by:KBerger
ID: 8201015
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
 
LVL 1

Expert Comment

by:manialok
ID: 8201124
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
 
LVL 27

Author Comment

by:Dabas
ID: 8206034
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question