How to show a custom list of record from array/List iterative process in ListView in C#

// another class to dynamic custom type references
class Person
    {
        public string firstName;
        public string middleName;
        public string lastName;

        public string GetFullName()
        {
            return firstName + " " + middleName + " " + lastName;
        }
    }
</pre>
// main class for showing the records in listView from a array/List&lt;&gt;
<pre>    public partial class MainUI : Form
    {

        List&lt;Person&gt; personsList = new List&lt;Person&gt;();
       

        public MainUI()
        {
            InitializeComponent();
        }

        
        private void addButton_Click(object sender, EventArgs e)
        {

            string firstName = firstNameTextBox.Text;
            string middleName = middleNameTextBox.Text;
            string lastName = lastNameTextBox.Text;

            Person newPerson = new Person();

            newPerson.firstName = firstName;
            newPerson.middleName = middleName;
            newPerson.lastName = lastName;

            personsList.Add(newPerson);

            firstNameTextBox.Clear();
            lastNameTextBox.Clear();
            middleNameTextBox.Clear();

            MessageBox.Show("saved");

            
        }

        private void showAllButton_Click(object sender, EventArgs e)
        {
            
            
            foreach (Person newPerson in personsList)
            {
                ListViewItem item = new ListViewItem();
                item.Text = newPerson.firstName;
                item.SubItems.Add(newPerson.middleName);
                item.SubItems.Add(newPerson.lastName);
                item.SubItems.Add(newPerson.GetFullName());

                recordBookListView.Items.Add(item);
            }

            
            
        }
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s