Return selected object in datagridview

1

I have the following parameterization of my datagridview:

    List<ModelAluno> alunos;
    ModelAluno aluno = new ModelAluno();

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

    }

    private void PesquisaAluno_Load(object sender, EventArgs e)
    {
        ConfiguraDataGrid();

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

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void ConfiguraDataGrid()
    {
        dg.Columns.Add("dg_Nome", "Nome");
        dg.Columns.Add("dg_Cpf", "CPF");
        dg.Columns.Add("dg_Curso", "Curso");
        dg.ReadOnly = true;
        dg.AllowUserToAddRows = false;

        foreach (DataGridViewColumn column in dg.Columns)
        {
            if (column.DataPropertyName == "Nome")
                column.Width = 300; //tamanho fixo da primeira coluna

            column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

    }

In the case, I'm getting a list of students from another form and I present in my datagridview only the fields I want (name, cpf and idcurso).

In the CellContentClick method I need to return the selected student to my previous form, but I can not return only those three fields, I need to return the mounted object (which is retrieved initially in a search in SQL Server and is already stored in the list received in the datagridview)

How do I return the entire object? Also, how do I, when pressing enter, I select the object that I want and present in the textbox of the previous form?

    
asked by anonymous 25.03.2018 / 16:40

1 answer

1

Changing slightly if DataGridView :

List<ModelAluno> alunos;
ModelAluno aluno = new ModelAluno();

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

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

private void ConfiguraDataGrid()
{
    //Essa parte você pode fazer pelo design do visual studio

    dg.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "Nome", Name = "columnName", DataPropertyName = "Nome" });

    dg.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "CPF", Name = "columnCpf", DataPropertyName = "Cpf" });

    dg.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "Curso", Name = "columnCurso", DataPropertyName = "Matricula.IdCurso" });

    dg.ReadOnly = true;
    dg.AllowUserToAddRows = false;
    dg.AutoGenerateColumns = false;
}

Double click event:

private void dg_CellMouseDoubleClick(object sender, CellMouseEventArgs e)
{
    aluno = alunos[e.RowIndex]; //o aluno que vocÊ clicou foi esse
}
    
25.03.2018 / 20:21