Creating Listview columns via code 
Author Message
 Creating Listview columns via code

When I try to create the columns programmatically, the control expects to
get a column width.  The documentation says a width of -1 will use the
widest item in the column and -2 will use the width of the column header.  I
cannot make either of these work.  All that works is setting an actual
width.

Can anyone help?

--
Bill McCarthy



Fri, 04 Mar 2005 04:30:45 GMT  
 Creating Listview columns via code
Hello Bill,

Quote:
> When I try to create the columns programmatically, the control
> expects to get a column width.  The documentation says a width
> of -1 will use the widest item in the column and -2 will use
> the width of the column header.  I cannot make either of these
> work.  All that works is setting an actual width.

    Setting ColumnHeader.Width to -1 or -2 will have no effect on an
'empty' ListView control.  Try executing the following code after you've
added some ListViewItems to your ListView control:

<Untested code>
Dim col As ColumnHeader

For Each col In lvwWhatever.ColumnHeaders
    col.Width = -2
Next col
</Untested code>

    This will resize the ColumnHeaders based on the current contents of
the ListView.  If you later add additional items you will need to
execute the above code again.

Hope this helps,
Len



Fri, 04 Mar 2005 08:43:14 GMT  
 Creating Listview columns via code
I have done it as you suggest.  It still didn't work.

--
Bill McCarthy


Quote:
> Hello Bill,

> > When I try to create the columns programmatically, the control
> > expects to get a column width.  The documentation says a width
> > of -1 will use the widest item in the column and -2 will use
> > the width of the column header.  I cannot make either of these
> > work.  All that works is setting an actual width.

>     Setting ColumnHeader.Width to -1 or -2 will have no effect on an
> 'empty' ListView control.  Try executing the following code after you've
> added some ListViewItems to your ListView control:

> <Untested code>
> Dim col As ColumnHeader

> For Each col In lvwWhatever.ColumnHeaders
>     col.Width = -2
> Next col
> </Untested code>

>     This will resize the ColumnHeaders based on the current contents of
> the ListView.  If you later add additional items you will need to
> execute the above code again.

> Hope this helps,
> Len



Fri, 04 Mar 2005 21:32:06 GMT  
 Creating Listview columns via code

Quote:
>I have done it as you suggest.  It still didn't work.

The following works for me.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
        ListView1.Columns.Add("Name", -2, HorizontalAlignment.Left).Width = -2
        ListView1.Columns.Add("Title", -2, HorizontalAlignment.Left).Width = -2
    End Sub

Thanks,
Peter Wu
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.



Sat, 05 Mar 2005 10:52:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. SQL Server 2000: Create column descriptions via code

2. ListView - set column width via program

3. removing code from a document created via code and save as

4. Changing column order in datagrid via code

5. Creating ListView Columns at Run-Time

6. Sample code needed - Listview - Move columns

7. ListView: Column Text becomes Left-Aligned when ColumnHeader Icon is changed in Code

8. ListView: Column Text becomes Left-Aligned when ColumnHeader Icon is changed in Code

9. Setting listview column widths in code

10. create Listview via API

11. Creating Listview Via CreateWindowEx

12. Create a Stored Procedure for MSDE via code?

 

 
Powered by phpBB® Forum Software