listview question

hi I added listview to my form now after clicking on different buttons I want listview to have different columns. This is my code:

ListView1.Columns.Add("File type", 20, HorizontalAlignment.Left)

ListView1.Columns.Add("Item Column", 20, HorizontalAlignment.Left)

ListView1.Columns.Add("Column 2",20, HorizontalAlignment.Left)

ListView1.Columns.Add("Column 3", 20, HorizontalAlignment.Left)

ListView1.Columns.Add("Column 4", 20, HorizontalAlignment.Center)

ListView1.Refresh()

ListView1.Update()

The problem is that after clicking the button nothing happens I don't see columns names!

What should I change?

[1017 byte] By [Bartosz] at [2007-12-23]
# 1
What's the View mode for the list view? Try setting it to Details.
SJWhiteley at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

it is set to details

When I put that code in while form is loading it works. But if I want to change columns names after clicking a button nothing happens

Bartosz at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Seems to work fine for me. I placed a listview and button in a form, pasted your code. Set the listview.view to details in design, run the application, press the button and the columns show up.

Is this in an existing application or in a brand new project? Try it in a new project.

SJWhiteley at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Have you tried manipulating the ColumnHeader collection instead of Columns
JRQ at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

Hi Bartosz,

As SJWhiteley said, it works fine for me too. I create a new project and add a button and a listview control into the form. Use the code below is ok for me. Maybe you can try a brand new project to have a luck.



private void button1_Click(object sender, EventArgs e)
{
if (flip == true)
{
ListView1.Columns.Clear();
ListView1.Columns.Add("File type", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("Item Column", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("Column 2", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("Column 3", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("Column 4", 20, HorizontalAlignment.Center);
ListView1.Refresh();
ListView1.Update();

flip = false;
}
else
{
ListView1.Columns.Clear();
ListView1.Columns.Add("NEW File type", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("NEW Item Column", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("NEW Column 2", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("NEW Column 3", 20, HorizontalAlignment.Left);
ListView1.Columns.Add("NEW Column 4", 20, HorizontalAlignment.Center);
ListView1.Refresh();
ListView1.Update();
flip = true;
}

gqlu at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...