M
M
MrLumuss2022-01-31 17:27:36
C++ / C#
MrLumuss, 2022-01-31 17:27:36

Why is the ListView not populating?

Hello! There is such a handler for filling the ListView

lvItems.Clear();

            using (var db = new InventContext())
            {
                var employee = db.Employee.Where(em => em.Name == selectedItem.Text);
                var emp = employee.First();

                var items = db.Item.Where(i => i.Employee == emp.id);

                if (items != null)
                {
                    foreach (var item in items)
                    {
                        ListViewItem lvItem = new ListViewItem(
                            new string[] {
                            item.id.ToString(),
                            item.ItemType.Name,
                            item.Name,
                            item.Count.ToString(),
                            item.DateOfIssue.ToString(),
                            item.DeliveryDate.ToString()
                            });


                        lvItems.Items.Add(lvItem);
                    }
                }
            }


in debugging, all variables have values, there are no voids, but the line is not added (
61f7f1a4a02f9227188134.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2022-02-01
@MrLumuss

if (items != null)
You can not write this at all, a deferred LINQ query always creates an object (IEnumerable, IQueryable).
try to substitute from the example, at least the list will be filled

private void seedListView()
        {
            //Define
            var data = new[]
            {
                new []{"Lollipop", "392", "0.2", "0"},
                new []{"KitKat", "518", "26.0", "7"},
                new []{"Ice cream sandwich", "237", "9.0", "4.3"},
                new []{"Jelly Bean", "375", "0.0", "0.0"},
                new []{"Honeycomb", "408", "3.2", "6.5"}
            };

            //Add
            foreach (string[] version in data)
            {
                var item = new ListViewItem(version);
                materialListView1.Items.Add(item);
            }
        }

M
Max, 2022-01-31
@MAKC77

var items = db.Item.Where(i => i.Employee == emp.id) .ToList() ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question