C# – Double click on DataGridView cell to open a form

cdatagridviewdouble-click

I have a form called ListaDeAlunos with a DataGridView on it.
When I double click on any cell, I want to open a form called Alunos for the selected row of the DataGridView on the form ListaDeAlunos, so I can edit the record. I almost got it to work, but the form Alunos does not show the right record when it opens.

Here's what I did so far:

On the source form I created a class called Variables
and in that class I created a public static string called RecordName

class Variables
{
    public static string RecordName;
}

On the source form I created a CellDoubleClick event:

    private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {          
        Form Alunos = new Alunos();
        Alunos.MdiParent = this.MdiParent;
        Alunos.Show();
        Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();
    }

On the second form (the one that will open in the DoubleClick event), I have the following code on the Form_Load event:

    private void Alunos_Load(object sender, EventArgs e)
    {   
     this.tbl_alunosBindingSource.Filter = string.Format("Nome LIKE '{0}%'", Variables.RecordName);              
    }

Any ideas on how to fix this? It's almost working!

Best Answer

PROBLEM SOLVED !!!

All I had to do was to put the last line of code on top. Simple.

private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{   Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();       
    Form Alunos = new Alunos();
    Alunos.MdiParent = this.MdiParent;
    Alunos.Show();        
}
Related Topic