Forum guidelines
AuthorQuestion
listviewitem constructor problem
Posted on: 14 Mar 2011
Hi All,
I'm learning c# so apologies if this is a stupid question

I get the error 'listviewitem does not contain a constructor that takes 1 arguments'.

The code I'm using is the following

            string[] lViewItem = new string[2];

            foreach (Collect c in collect)
            {
                lViewItem[0] = c.Id.ToString();
                lViewItem[1] = c.Name;

                ListViewItem lvi = new ListViewItem(lViewItem);
               

                listView1.Items.Add(lvi);

What am I doing wrong.

Thanks in advance.

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: listviewitem constructor problem
Posted on: 15 Mar 2011   Accepted Answer
I think everyone's been assuming that it's a Windows Forms application but it's clear now from the full error message that it's a WPF application where the constructor for ListViewItem doesn't take any parameters.

I'd create a custom class:

public class Data
{
    public string Id { get; set; }
    public string Name { get; set; }

    public Data(string id, string name)
    {
        Id = id;
        Name = name;
    }    
}

You can then do:

foreach (Collect c in collect)
{
    Data d = new Data(c.Id.ToString(), c.Name);             
    listView1.Items.Add(d);
}

Alternatively, you could try using an anonymous type rather than a custom type:

foreach (Collect c in collect)
{
    var d = new{Id = c.Id.ToString(), Name = c.Name};             
    listView1.Items.Add(d);
}

Re: listviewitem constructor problem
Posted on: 14 Mar 2011  
What is collect
Iain
  • 0
  • 0
Re: listviewitem constructor problem
Posted on: 14 Mar 2011  
I have a class called collect and in this case it is Collection<collect> collect

I should have probably called it something different but it suited my needs at the time

Re: listviewitem constructor problem
Posted on: 14 Mar 2011  
to add to listview 
ListViewItem lvt = new ListViewItem(item name);

lvt.Tag = (item name);
lvt.Text = (item name);
listView1.Items.Add(lvt);

Upload your whole project if you can't make it work

Re: listviewitem constructor problem
Posted on: 14 Mar 2011  
Copy the error message from the Output window and paste it here. The message you posted is not accurate; listviewitem is not a .Net class (ListViewItem is). If you have defined a class named listviewitem then look at it; the error message is about it.
Thinking is a feeling; pleasant for some and unpleasant for others.
Iain
  • 0
  • 0
Re: listviewitem constructor problem
Posted on: 15 Mar 2011  
Error    1    'System.Windows.Controls.ListViewItem' does not contain a constructor that takes 1 arguments   

This is the error I get in the output window


Re: listviewitem constructor problem
Posted on: 15 Mar 2011  
The fact that it is WPF is exactly why the complete and accurate error message is critical, especially when the thread is created in an ambiguous forum. This thread is in the C# Language forum but the question is not about the C# Language. There is a WPF forum and the question belongs there. With the complete and accurate error message, it is easy to see what the error message is trying to tell us. If Iain had just looked at the documentation, it is very easy to see what we (Frogleg and I and the compiler) see.

Thinking is a feeling; pleasant for some and unpleasant for others.
Iain
  • 0
  • 0
Re: listviewitem constructor problem
Posted on: 15 Mar 2011  
Thanks, for your help, apologies for not making myself clearer.

I shall take more care where I post in future. 

It is a struggle getting through the documentation for someone learning this like myself.  I shall try and understand it better before posting in future.

Thanks for your help, it's appreciated.

Re: listviewitem constructor problem
Posted on: 15 Mar 2011  
Try doing the following if you are not yet familiar with doing this. In your program's source code in Visual Studio, click on ListViewItem (the class name) and then press F1. That should open a browser window showing the ListViewItem documentation. And voila, as simple as that, you can see the relevant constructors. The frustrating thing is that if VS is showing an error for that line, then the stupid VS shows the documentation of the error message instead of the documentation of the class. This is another example of inconveniences caused by Microsoft's misguided attempt to make things more convenient.

Thinking is a feeling; pleasant for some and unpleasant for others.
Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.