21

I'm learning how to use the listView in a windowsForm and I have some problems that I hope to solve here. The first thing is when I'm creating the columns with the code below:

private void initListView()
    {
        // Add columns
        lvRegAnimals.Columns.Add("Id", -3,HorizontalAlignment.Left);
        lvRegAnimals.Columns.Add("Name", -3, HorizontalAlignment.Left);
        lvRegAnimals.Columns.Add("Age", -3, HorizontalAlignment.Left);
    }

When I run the program, the name of the columns are not visible, they are all at the left corner, and I have to "drag" them to be able to read the text. What have I done wrong?

And finally I wonder how I add items to the columns. Do I first create a object like

ListViewItem item1 = new ListViewItem(???);
item1.SubItems.Add("text");

Is each listViewItem objects a column or a row? How do I add rows of info? Preciate some help! Thanks!

4 Answers 4

47

Your first problem is that you are passing -3 to the 2nd parameter of Columns.Add. It needs to be -2 for it to auto-size the column. Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.columns.aspx (look at the comments on the code example at the bottom)

private void initListView()
{
    // Add columns
    lvRegAnimals.Columns.Add("Id", -2,HorizontalAlignment.Left);
    lvRegAnimals.Columns.Add("Name", -2, HorizontalAlignment.Left);
    lvRegAnimals.Columns.Add("Age", -2, HorizontalAlignment.Left);
}

You can also use the other overload, Add(string). E.g:

lvRegAnimals.Columns.Add("Id");
lvRegAnimals.Columns.Add("Name");
lvRegAnimals.Columns.Add("Age");

Reference for more overloads: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.columnheadercollection.aspx

Second, to add items to the ListView, you need to create instances of ListViewItem and add them to the listView's Items collection. You will need to use the string[] constructor.

var item1 = new ListViewItem(new[] {"id123", "Tom", "24"});
var item2 = new ListViewItem(new[] {person.Id, person.Name, person.Age});
lvRegAnimals.Items.Add(item1);
lvRegAnimals.Items.Add(item2);

You can also store objects in the item's Tag property.

item2.Tag = person;

And then you can extract it

var person = item2.Tag as Person;

Let me know if you have any questions and I hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help! I'm going to try this code later or tomorrow. Perhaps I can return to you if I have some questions about it.
it does not have any overload constructor for implicitly typed string array ?
@Charlie, I am assuming you are referring to the overload constructors of ListViewItem? If you want to create a ListViewItem with SubItems from the constructor, you can either use the string[] overload or ListViewSubItem[] overload. I am using new[] instead of new string[] because C# can automatically infer the type (which is string in this case). See msdn.microsoft.com/en-us/library/…
You should add lvRegAnimals.View = View.Details; (or set the View property in the designer). Otherwise you only see the values of the first column. ..... Additionally I had trouble with auto size (-2) of all columns. When I set a fixed size to one column, the view showed all columns.
43

I didn't see anyone answer this correctly. So I'm posting it here. In order to get columns to show up you need to specify the following line.

lvRegAnimals.View = View.Details;

And then add your columns after that.

lvRegAnimals.Columns.Add("Id", -2, HorizontalAlignment.Left);
lvRegAnimals.Columns.Add("Name", -2, HorizontalAlignment.Left);
lvRegAnimals.Columns.Add("Age", -2, HorizontalAlignment.Left);

Hope this helps anyone else looking for this answer in the future.

Comments

6

You need to set property for the control:

listView1.View = View.Details;

3 Comments

what does this property do >/
what does this property do - ListViews are used in the standard "Windows Explorer", so imagine Details view in the standard folder/file browser.
It will show grid structure. Pls. try it for yourself.
0
            listView1.View = View.Details;
        listView1.Columns.Add("Target No.", 83, HorizontalAlignment.Center);
        listView1.Columns.Add("   Range   ", 100, HorizontalAlignment.Center);
        listView1.Columns.Add(" Azimuth ", 100, HorizontalAlignment.Center);     

i also had same problem .. i drag column to left .. but now ok .. so let's say i have 283*196 size of listview ..... We declared in the column width -2 for auto width .. For fitting in the listview ,we can divide listview width into 3 parts (83,100,100) ...

1 Comment

Don't just post code, you need to add an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.