How to Populate listview with dictionary data

José Carlos 886 Reputation points
2023-02-17T10:50:55.7766667+00:00

Friends.

I have a form with two text boxes. One for the dictionary key and one for the value. I fill in these values and click a button to add to the dictionary. Until then it is working perfectly. The dictionary is being populated. But I would like to show the listView at the same time as filling it out. I did the steps below, but the values do not come out in the listview. Exit System.Collections...

At the beginning of the program I define the item as ListViewItem item;

I've made many attempts, but since I'm using the dictionary for the first time, I can't figure out the error.

Thanks

Follow the code below.



Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,448 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,339 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,556 Reputation points
    2023-02-17T12:28:20.07+00:00

    See full source code.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace ListViewBasic
    {
        public partial class Form1 : Form
        {
            private readonly Dictionary<string, string> _dictionary = 
                new Dictionary<string, string>();
    
            public Form1()
            {
                InitializeComponent();
                listView1.MouseDoubleClick += ListView1_MouseDoubleClick;
    
                _dictionary.Add("A1", "111");
                _dictionary.Add("B1", "222");
                _dictionary.Add("C1", "333");
    
                foreach (var item in _dictionary)
                {
                    listView1.Items.Add(
                        new ListViewItem(new[] { item.Key, item.Value })
                        {
                            Tag = item.Key
                        });
                }
    
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                listView1.Items[0].Selected = true;
                ActiveControl = listView1;
            }
    
            private void ListView1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (listView1.Items.Count == 0)
                {
                    return;
                }
    
                MessageBox.Show(
                    $"Key {listView1.SelectedItems[0].Text} Value {listView1.SelectedItems[0].SubItems[1].Text}");
            }
    
            private void AddButton_Click(object sender, System.EventArgs e)
            {
                if (!_dictionary.ContainsKey(KeyTextBox.Text))
                {
                    listView1.Items.Add(
                        new ListViewItem(new []{KeyTextBox.Text, ValueTextBox.Text})
                        {
                            Tag = KeyTextBox.Text
                        });
    
                    _dictionary.Add(KeyTextBox.Text, ValueTextBox.Text);
    
                    KeyTextBox.Text = "";
                    ValueTextBox.Text = "";
                }
                else
                {
                    MessageBox.Show("dup key");
                }
            }
    
            private void IterateButton_Click(object sender, System.EventArgs e)
            {
                var items = listView1.SelectedItems;
                if (items.Count > 0)
                {
                    foreach (ListViewItem item in items)
                    {
                        Console.WriteLine($@"{item.Text,-10}{item.SubItems[1].Text}");
                    }
                }
            }
        }
    }
    

    figure1

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. José Carlos 886 Reputation points
    2023-02-17T21:15:36.2+00:00

    Perfect Karen.

    Thank you very much. It helped a lot.

    jose carlos

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.