Added multiple items in a DataGrid by List

1

I have two textboxes. One for e-mail and the other for remarks. When the user clicks the add button, it should be adding to my datagrid, but to no avail! How can I do this? As the user clicks on the add button, he has to increase the rows of the datagrid

Note: I use WPF

I'm tempting this way:

public class Email{

     public string email { get; set; }
     public string obs { get; set; }
    }

for (int i=0; i < 10; i++){

     List<Email> lista = new List<Email>();
     Email em = new Email();
     em.email = textEmpEmail.Text;
     em.obs = textEmpObs1.Text;
     lista.Add(em);

     dataGridEmails.ItemsSource = lista;
}

In this way, it inserts 10 at a time ... But I needed that added per click, one at a time ..

Thank you!

    
asked by anonymous 27.05.2015 / 19:20

1 answer

1

You can add the values as follows.

 dataGridView1.Rows.Add();
 dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[0].Value = textBox1.Text;
 dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[1].Value = textBox2.Text;

Now if you want to add the list all at once, create the columns in your grid with the "DataPropertyName" equal to the name of the properties of your class and go that way.

 List<Email> lista = new List<Email>();
 Email em = new Email();
 em.email = textBox1.Text;
 em.obs = textBox2.Text;
 lista.Add(em);

 dataGridView1.DataSource = lista;
    
27.05.2015 / 19:29