Error inserting data into datagridview

0

I have the following code:

public partial class PesquisaAluno : Form
{
    List<ModelAluno> alunos;

    public PesquisaAluno(List<ModelAluno> alunos)
    {
        InitializeComponent();
        this.alunos = alunos;
    }

    private void PesquisaAluno_Load(object sender, EventArgs e)
    {
        dg.DataSource = alunos;

        foreach (var aluno in alunos)
        {
            dg.Rows.Add(aluno.Nome, aluno.Cpf, aluno.Matricula.IdCurso);
        }

    }

This code is called when searching in the previous form returns more than 1 item. These results are stored in the students list and passed to the datagrid form. However, when the form tries to load, it returns the following error:

System.InvalidOperationException: 'You can not programmatically add rows to the DataGridView row collection when the control is bound to data.'

How do I resolve this error?

    
asked by anonymous 24.03.2018 / 19:19

0 answers