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