user3200169
user3200169

Reputation: 275

How to make the columns in a ListView expand dynamically?

ColumnHeader header = new ColumnHeader();
header.Text = "";
header.Name = "col1";

listView1.Columns.Add(header);
listView1.View = View.Details;
listView1.Scrollable = true;

readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");

lines = Regex.Split(readableRss, "\r\n")
                   .Where(str => !string.IsNullOrEmpty(str))
                   .ToList();

for (int i = 0; i < lines.Count; i++)
{
    listView1.Items.Add(lines[i]);
}

This is what im getting:

enter image description here

I need to use the mouse to move the Column/s to the right so i will see all the line/s. How can i make it automatic so each line the column will be spread/open to the right so it will show the entire line ?

Adding this line: header.Width = 1000; solve it. But how do i know how much to set it to ? 1000 is good in this case but why not 999 or 1500 ? I just guessed that the width should be 1000

Upvotes: 0

Views: 260

Answers (2)

Szymon
Szymon

Reputation: 43023

Add that line to make the column resize based on content:

header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);

You need to add it after you have added all the items.

Upvotes: 2

rae1
rae1

Reputation: 6144

You need to use the ColumnHeader's AutoResize method, which will,

Resize the width of the column as indicated by the resize style.

So, something like this,

header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);

Upvotes: 2

Related Questions