locked
ListView no definion for items RRS feed

  • Question

  • Hi Gang
    
    I’m using linq to sql:

    This line says - ListView does bot contain a definition for items
    ListView.items.Add(item);
    
    private void button1_Click(object sender, EventArgs e)
    {
        Cursor.Current = Cursors.WaitCursor;
    
        using(DbBaseballDataContext db = new DbBaseballDataContext())
        {
            List<Batting> list = db.Battings.ToList();
            foreach(Batting b in list)
            { 
                ListViewItem item = new ListViewItem(b.Id.ToString());
                item.SubItems.Add(b.NAME);
                item.SubItems.Add(b.POS.ToString());
                ListView.items.Add(item); // Error Here
                    }
            Cursor.Current = Cursors.Default;
        }
    }
    
    Thanks for any help
    
    JereTheBear
        
    

    Thursday, February 20, 2020 11:44 PM

Answers

  • TommyTwoTrain

    it was Listview1 - should be listview1

    Thanks for the help

    JereTheBear

    • Marked as answer by JereBerar Friday, February 21, 2020 3:21 AM
    Friday, February 21, 2020 3:21 AM

All replies


  • Make sure the name of the control is correct the default is listView1?

        listView1.Items.Add(item);

    PS also watch upper lower case ie .Items not items.
    Friday, February 21, 2020 12:58 AM
  • Hello,

    The following uses Entity Framework Core (works the same with Entity Framework classic)

    using System;
    using System.Linq;
    using System.Windows.Forms;
    using NorthWindContactsCodeFirst.Contexts;
    
    namespace NorthWindContactsCodeFirst
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                listView1.View = View.Details;
    
                listView1.GridLines = true;
                listView1.OwnerDraw = false;
                listView1.FullRowSelect = true;
    
                //Add column header
                listView1.Columns.Add("NameColumn", 150);
                listView1.Columns.Add("VersionColumn", 130);
    
    
                listView1.Columns[0].Text = "First name";
                listView1.Columns[1].Text = "Last name";
    
    
                using (NorthContext context = new NorthContext())
                {
    
                    var contacts = context.Contacts.ToList();
                    foreach (var contact in contacts)
                    {
                        var item = new ListViewItem(new []
                        {
                            contact.FirstName,
                            contact.LastName,
                            contact.ContactId.ToString()
                        });
                        listView1.Items.Add(item);
                    }
                }
    
                listView1.Items[0].Selected = true;
                listView1.Select();
                CurrentButton.Enabled = true;
            }
    
            private void CurrentButton_Click(object sender, EventArgs e)
            {
                var selected = listView1.Items.Cast<ListViewItem>()
                    .Where(listViewItem => listViewItem.Selected)
                    .Select(listViewItem => listViewItem)
                    .FirstOrDefault();
    
                if (selected != null)
                {
                    MessageBox.Show($"{selected.SubItems[0].Text}, {selected.SubItems[1].Text}, {selected.SubItems[2].Text}");
                }
                
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, February 21, 2020 2:13 AM
  • Hi JereBerar,

    Thank you for posting here.

    I have some questions.

    Is it a compile-time exception or a runtime exception? What is the type of exception? Could you please show us those error messages?

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 21, 2020 2:37 AM
  • TommyTwoTrain

    it was Listview1 - should be listview1

    Thanks for the help

    JereTheBear

    • Marked as answer by JereBerar Friday, February 21, 2020 3:21 AM
    Friday, February 21, 2020 3:21 AM
  • TommyTwoTrain

    it was Listview1 - should be listview1

    Thanks for the help

    JereTheBear

    So what was this exactly?   A pop quiz?  In your OP you specifically state:

    This line says - ListView does bot contain a definition for items
    ListView.items.Add(item);

    The problem is that if you did indeed have an instance of a ListView named ListView, items should be capitalized.  If instead you had an instance of ListView named listView you'd have gotten a completely different error message.

    I'm reporting your posts as abuse/spam.  Your behavior here is ridiculous.

    Friday, February 21, 2020 9:06 AM