Dabas
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(oPrintOrd ers)
lvJobs.Columns.Clear()'Thi s works OK if I leave the two columns I added at design time!
For Each dc In oPrintOrders.Tables(0).Col umns
'This also did not work
'ch = lvJobs.Columns.Add(dc.Capt ion.ToStri ng, -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).Row s()
li = lvJobs.Items.Add(oDR.Item( "JobNumber "))
li.SubItems.Add(oDR.Item(" DeliveryCo de"))
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
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(oPrintOrd
lvJobs.Columns.Clear()'Thi
For Each dc In oPrintOrders.Tables(0).Col
'This also did not work
'ch = lvJobs.Columns.Add(dc.Capt
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).Row
li = lvJobs.Items.Add(oDR.Item(
li.SubItems.Add(oDR.Item("
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("Tes t",300,Hor izontalAli gnment.Lef t)
Me.lvTest.Columns.Add("Tes t2",300,Ho rizontalAl ignment.Le ft)
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! ;-)
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("Tes
Me.lvTest.Columns.Add("Tes
Dim oItem As ListViewItem
oItem = Me.lvTest.Items.Add("Test"
oItem.SubItems.Add("test2"
Me.lvTest.Columns(0).Width
Me.lvTest.Columns(1).Width
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
oDR is not defined
make sure view property of listview is set to details
rest is fine and should work
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!
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!
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