Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Trouble formatting columns width for a ListView

Hi,

I am trying to format the column widths for a ListView but it's not working.  Is this a bug in Windows?  I use C#.NET Framework 2.0.
The first two columns are very narrow and the third is very wide, taking up the remaining unused space.

Here's the code.

Thanks,
newbieweb

addColumns is true the first time I create the dialog.

               listView.SuspendLayout();
                if (addColumns)
                {
                    // Create columns for the items and subitems.
                    listView.Columns.Add("#", (int)0.15 * listView.Width, HorizontalAlignment.Left);
                    listView.Columns.Add("Street", (int)0.55 * listView.Width, HorizontalAlignment.Left);
                    listView.Columns.Add("Complete In", (int)0.25 * listView.Width, HorizontalAlignment.Left);

                    ListViewItem item1 = new ListViewItem("0");
                    for (int ii = 0; ii < listView.Columns.Count; ii++)
                    {
                        listView.Columns[ii].Width = -2;
                    }                  
                }
               
                if (orderStatusList != null && orderStatusList.Count > 0)
      {
            foreach ( Order order in orderStatusList )
            {
                  ListViewItem item1 = new ListViewItem(order.OrderNum);
                  item1.SubItems.Add(order.Street);
                  item1.SubItems.Add(order.CompletionMinutes.ToString());
                  listView.Items.Add( item1 );
            }
      }
            
      listView.ResumeLayout();
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

where are you putting this code?
I use a different approach, I create the columns at the Load event, and resize them at the form's Size event, so if you or the user change the size of the form, proportion is keeped.
Also you have to decrement a couple of pixels from the listView.Width ('cause of the margin) to avoid the horizontal bar to appear.
Avatar of curiouswebster

ASKER

Would you be so kind as to provide me the code for this?

When you create the columns, which constructor do you use?

When you size them, how do you do that exactly?

Thanks,
newbieweb
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

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
That was the trick!

Thanks,
newbieweb