Using Cellclick to load data from a record

Asked

Viewed 248 times

0

I’m doing a C# application in Visual Studio Windows Forms without using a database, I’m controlling everything for a class (Person) and two more classes (Personal and Personal) inherited from the base class.

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

I’m saving the registration data on a list, 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’t figure out how to do that and I also saw that the CellClick. I am not posting the code because there is no error because I am not sure how to implement the code this function Rowselectionchanged to display the data registered by the textbox at the moment I click on the selected line.

I saw you have on the date Event here on this site: Rowselectionchanged Event (Webhierarchicaldatagrid) saying that there is Currentselectedrows and Previousselectedrows and would like to know if one of these really meets what I need.

Updating:

You are giving error in the save part, it is in this part to get 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 on the grid appears only the name of the registered user inserir a descrição da imagem aqui

  • Please be more specific. We have no way of knowing what problem you are having if you don’t put the code you are using, tell what the expected result is and what result is being produced so far. Also, don’t use the tag visual-studio if your problem is not directly related to the IDE. You can see more details about IDE, compilers and programming languages in this question here of the Stack Overflow in Portuguese.

  • The error in saving may be caused by dataGridView1.CurrentRow.Index, since you don’t necessarily have a line selected when the user clicks on the save button. The question is very confusing. I thought the problem was about how to rescue the Pessoa selected, but now it seems like saving to the list. What is the question?

  • It’s just that I’ve been updating the question. But in this case the selected person was also a question and you answered (very well, great solution) but also need to know how to get the index that is represented by x here -> dataGridView1.Rows.Add(listcadastro[x].name) (the registration data is already being saved in an array list that I put there in the code). In short, in datagrid I just want to show the name of each registered user.

3 answers

1

if (dgv_pessoa.RowCount > 0)
        {
            TuaTextBox.Text =  dgv_pessoa[0, dgv_pessoa.CurrentRow.Index].Value.ToString();                 
        }

zero (0) is the culuna index.

place in the event Cellclick or Celldoubleclick.

  • for example, in the case of dgv_person[0... I will need to use the others, then how do I do? I updated the question with the part of the code with the save button and the Cellclick

0

I think it would look good. Explaining: As you added an object to the grid, you at the time to call the line can give a cast by converting it to that object and calling the property you want.

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

0


To identify the Pessoa selected you can recover directly 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);
  • And to display only the name of the registered user in the grid list, as I do to get the index x(nonfinite) that appears here -> dataGridView1.Rows.Add(listcadastro[x]. name) ?

  • Dude, I’m sorry, but I still don’t get it. You want to add the entire list on the grid or you want to add a newly created person?

  • I added all the data in the array list already, but I want to show on the grid only the first data entered (which was the name). And what you answered I will use to show the rest of the data back in the textbox on the line that was selected (in the name that was selected).

  • Got it. What about Rows.Add()? You want to add what?

  • I just want to add the name information on the grid, then I tried to access the index but x is undefined and I don’t know how to indicate that there at x is the index of the selected line

  • I edited the answer. See if that’s what you want, please

  • It is that on the grid I just want to display one of the data I typed, that of the name text box, only it will appear on the grid. And then when I click on the line with the mouse, then yes other information will appear in the textbox showing the registration data.

  • And isn’t that what the code I posted does? hehe

  • But it gives error from the new Person(); and can do what I want only using dataGridView1.Rows.Add(clients[x].name) but I do not know how to define the x that is the index of the line

  • I edited again. See if this is it, please

  • 1

    Yes! Thank you very much man, vlw even! I learned a lot from you, thank you very much!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.