Using CellClick to load data from a record

0

I'm doing a C # application in Windows Forms from Visual Studio without using a database, I'm handling everything by one class (Person) and two other classes (PersonFisica and PersonJuridica) inherited from the base class.

I'm trying to load DataGrid with the data of a register that I make in the program at the moment I click on the selected line, but I'm not getting it (what initially needs to appear in the lines of DataGrid is the first data and then when you select it with a click there yes all the data back in the TextBox where they were inserted). For example, I show the name of each user registered in the rows of the datagrid and when I click on a row, I want the registered data to be shown back in each textbox where it was inserted.

I am saving the data of the cadastre in a list, in this way

List<Pessoa> listcadastro = new List<Pessoa>();
listcadastro.Add(new Pessoa(/*aqui eu coloco cada text box que contem os dados que eu quero*/)); 

But I can not figure out how to do this, and I also saw that CellClick can be used. I am not posting the code because there is no error occurring because I am not sure how to implement the code this RowSelectionChanged function to display the data registered by the textbox at the moment I click on the selected row.

I saw what you have about the event date here on this site: RowSelectionChanged Event (WebHierarchicalDataGrid) saying that there is the CurrentSelectedRows and PreviousSelectedRows and I would like to know if one of these actually caters to what I need.

Update:

You're giving the error in the save part, it's in that part of getting the index

List<Pessoa> listcadastro = new List<Pessoa>();

//exibir todos os nomes que foram cadastrados um em baixo do outro na grid
       int Row;
private void bt_salvar_Click_1(object sender, EventArgs e)
{

    listcadastro.Add(new Pessoa(txt_nome.Text, txt_endereco.Text, int.Parse(txt_ano.Text), txt_telefone.Text));
    Row = dataGridView1.CurrentRow.Index;
    dataGridView1.Rows.Add(listcadastro[Row].Nome);
}

//mostrar todos os dados de volta na textbox em que foi inserido quando selecionar a linha 
public void DataGridView1_CellClick(object sender, EventArgs e)
{
    if (dataGridView1.RowCount > 0)
    {
        txt_nome.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
        txt_endereco.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
        txt_ano.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
        txt_telefone.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();

    }
}

Here below I made an assembly showing how it is to stay, there in the grid only the name of the registered user appears  

    
asked by anonymous 06.03.2017 / 13:51

3 answers

0

To identify% selected% you can retrieve direct from your list through the index of the selected line:

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
    Pessoa pessoaSelecionada = listcadastro[datagridview1.CurrentCell.RowIndex];
    txt_nome.Text = pessoaSelecionada.Nome;
    txt_endereco.Text = pessoaSelecionada.Endereco;
    txt_ano.Text = pessoaSelecionada.Ano.ToString();
    txt_telefone.Text = pessoaSelecionada.Telefone;
}

To add to the grid the way you are implementing, you can do:

listcadastro.Add(new Pessoa(txt_nome.Text, txt_endereco.Text, int.Parse(txt_ano.Text), txt_telefone.Text));
dataGridView1.Rows.Add(listcadastro.Last().Nome);
    
06.03.2017 / 18:18
1
if (dgv_pessoa.RowCount > 0)
        {
            TuaTextBox.Text =  dgv_pessoa[0, dgv_pessoa.CurrentRow.Index].Value.ToString();                 
        }

zero (0) is the index of the culuna.

put in the CellClick or CellDoubleClick event.

    
06.03.2017 / 17:02
0

I think it would look good this way. Explaining: As you added an object to the grid, you can cast a line by casting it to the object and calling the property you want.

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
    textbox1.Text = ((Pessoa)DataGridView1.CurrentRow.DataBoundItem).endereco;
}
    
08.03.2017 / 16:56