1

I have a C# WPF application that has a form with two fields. Every time the form is submitted, I want to get the values and use the Instructor class to add the new item to a list. Then, I want to loop through the list and display the items in the ListView element. I realize I can do this without the class, but having the class is a requirement for my school assignment.

Here is my Main Window class:

public partial class MainWindow : Window
    {
        private List<Instructor> instList;
        public MainWindow()
        {
            InitializeComponent();
            List<Instructor> instList = new List<Instructor> { };
        }

        private void btnCreateInstructor_Click(object sender, RoutedEventArgs e)
        {
            spCreateInstructor.Visibility = (spCreateInstructor.Visibility == Visibility.Hidden) ? Visibility.Visible : Visibility.Hidden;
        }

        private void btnInstructorSubmit_Click(object sender, RoutedEventArgs e)
        {
            instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
            foreach (var inst in instList)
            {
                lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName);  
               //Error occurs on the line above.
            }
        }
    }

This is the instructor class:

class Instructor
    {
        public string firstName { set; get; }
        public string lastName { set; get; }
    }

My problem is that I get an error saying No overload for method Add takes 3 arguments What am I doing wrong? I have indicated where the error occurs with a comment in the code.

ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81

4 Answers4

0

Try replacing this line:

lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName); 

with this one

lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName }); 

It is similar to how you added to the instList.

Edit

After realizing lvInstructorList is a ListView xaml element, this should work:

var listViewItem = new ListViewItem(new Instructor { firstName = inst.firstName, lastName = inst.lastName }); 
lvInstructorList.Items.Add(listViewItem);
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
0

ah here is how to create a listview and add items to it in wpf check this question Add Items to Columns in a WPF ListView

In short

list view takes an object so you need to add items like that

lvInstructorList.Items.Add(lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName });

and you should bind to it in xaml

<ListView x:Name="lvInstructorList">
<ListView.View>
    <GridView>
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding firstName }"/>
    </GridView>
</ListView.View>

Community
  • 1
  • 1
Amr Alaa
  • 1,156
  • 11
  • 16
0

Not sure why you want to loop again and again to the lvInstructorList. But you have to clear every time.

instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
    lvInstructorList.Clear();                
    foreach (var inst in instList)
    {
        lvInstructorList.Items.Add(inst); 
    }
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
0

It looks to me like you are not intending on binding the list view to an object, but rather just insert string values into the list view. If that is the case:

lvInstructorList.Items.Add(string.Format("{0} {1}", inst.firstName, inst.lastName));